Module: FactoryBot::Syntax::Methods

Included in:
Default, FactoryBot::SyntaxRunner
Defined in:
lib/factory_bot/syntax/methods.rb

Overview

This module is a container for all strategy methods provided by FactoryBot. This includes all the default strategies provided (#build, #create, #build_stubbed, and #attributes_for), as well as the complementary *_list and *_pair methods.

Examples:

singular factory execution

# basic use case
build(:completed_order)

# factory yielding its result to a block
create(:post) do |post|
  create(:comment, post: post)
end

# factory with attribute override
attributes_for(:post, title: "I love Ruby!")

# factory with traits and attribute override
build_stubbed(:user, :admin, :male, name: "John Doe")

multiple factory execution

# basic use case
build_list(:completed_order, 2)
create_list(:completed_order, 2)

# factory with attribute override
attributes_for_list(:post, 4, title: "I love Ruby!")

# factory with traits and attribute override
build_stubbed_list(:user, 15, :admin, :male, name: "John Doe")

Instance Method Summary collapse

Instance Method Details

#attributes_for(name, *traits_and_overrides, &block) ⇒ Hash

(see #strategy_method) Generates a hash of attributes for a registered factory by name.

Returns:

  • (Hash)

    hash of attributes for the factory



# File 'lib/factory_bot/syntax/methods.rb', line 49

#attributes_for_list(name, amount, *traits_and_overrides, &block) ⇒ Array<Hash>

(see #strategy_method_list)

Returns:

  • (Array<Hash>)

    array of attribute hashes for the factory



# File 'lib/factory_bot/syntax/methods.rb', line 66

#attributes_for_pair(name, *traits_and_overrides, &block) ⇒ Array<Hash>

(see #strategy_method_pair)

Returns:

  • (Array<Hash>)

    pair of attribute hashes for the factory



# File 'lib/factory_bot/syntax/methods.rb', line 82

#build(name, *traits_and_overrides, &block) ⇒ Object

(see #strategy_method) Builds a registered factory by name.

Returns:

  • (Object)

    instantiated object defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 33

#build_list(name, amount, *traits_and_overrides, &block) ⇒ Array

(see #strategy_method_list)

Returns:

  • (Array)

    array of built objects defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 54

#build_pair(name, *traits_and_overrides, &block) ⇒ Array

(see #strategy_method_pair)

Returns:

  • (Array)

    pair of built objects defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 70

#build_stubbed(name, *traits_and_overrides, &block) ⇒ Object

(see #strategy_method) Builds a stubbed registered factory by name.

Returns:

  • (Object)

    instantiated object defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 44

#build_stubbed_list(name, amount, *traits_and_overrides, &block) ⇒ Array

(see #strategy_method_list)

Returns:

  • (Array)

    array of stubbed objects defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 62

#build_stubbed_pair(name, *traits_and_overrides, &block) ⇒ Array

(see #strategy_method_pair)

Returns:

  • (Array)

    pair of stubbed objects defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 78

#create(name, *traits_and_overrides, &block) ⇒ Object

(see #strategy_method) Creates a registered factory by name.

Returns:

  • (Object)

    instantiated object defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 39

#create_list(name, amount, *traits_and_overrides, &block) ⇒ Array

(see #strategy_method_list)

Returns:

  • (Array)

    array of created objects defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 58

#create_pair(name, *traits_and_overrides, &block) ⇒ Array

(see #strategy_method_pair)

Returns:

  • (Array)

    pair of created objects defined by the factory



# File 'lib/factory_bot/syntax/methods.rb', line 74

#generate(name) ⇒ Object

Generates and returns the next value in a sequence.

Arguments:

name: (Symbol)
  The name of the sequence that a value should be generated for.

Returns:

The next value in the sequence. (Object)


113
114
115
# File 'lib/factory_bot/syntax/methods.rb', line 113

def generate(name)
  Internal.sequence_by_name(name).next
end

#generate_list(name, count) ⇒ Object

Generates and returns the list of values in a sequence.

Arguments:

name: (Symbol)
  The name of the sequence that a value should be generated for.
count: (Fixnum)
  Count of values

Returns:

The next value in the sequence. (Object)


127
128
129
130
131
# File 'lib/factory_bot/syntax/methods.rb', line 127

def generate_list(name, count)
  (1..count).map do
    Internal.sequence_by_name(name).next
  end
end