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.
Instance Method Summary collapse
-
#attributes_for(name, *traits_and_overrides, &block) ⇒ Hash
(see #strategy_method) Generates a hash of attributes for a registered factory by name.
-
#attributes_for_list(name, amount, *traits_and_overrides, &block) ⇒ Array<Hash>
(see #strategy_method_list).
-
#attributes_for_pair(name, *traits_and_overrides, &block) ⇒ Array<Hash>
(see #strategy_method_pair).
-
#build(name, *traits_and_overrides, &block) ⇒ Object
(see #strategy_method) Builds a registered factory by name.
-
#build_list(name, amount, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_list).
-
#build_pair(name, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_pair).
-
#build_stubbed(name, *traits_and_overrides, &block) ⇒ Object
(see #strategy_method) Builds a stubbed registered factory by name.
-
#build_stubbed_list(name, amount, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_list).
-
#build_stubbed_pair(name, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_pair).
-
#create(name, *traits_and_overrides, &block) ⇒ Object
(see #strategy_method) Creates a registered factory by name.
-
#create_list(name, amount, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_list).
-
#create_pair(name, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_pair).
-
#generate(*uri_parts, scope: nil) ⇒ Object
Generates and returns the next value in a global or factory sequence.
-
#generate_list(*uri_parts, count, scope: nil) ⇒ Object
Generates and returns the list of values in a global or factory sequence.
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.
|
# File 'lib/factory_bot/syntax/methods.rb', line 49
|
#attributes_for_list(name, amount, *traits_and_overrides, &block) ⇒ Array<Hash>
(see #strategy_method_list)
|
# File 'lib/factory_bot/syntax/methods.rb', line 66
|
#attributes_for_pair(name, *traits_and_overrides, &block) ⇒ Array<Hash>
(see #strategy_method_pair)
|
# 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.
|
# File 'lib/factory_bot/syntax/methods.rb', line 33
|
#build_list(name, amount, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_list)
|
# File 'lib/factory_bot/syntax/methods.rb', line 54
|
#build_pair(name, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_pair)
|
# 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.
|
# File 'lib/factory_bot/syntax/methods.rb', line 44
|
#build_stubbed_list(name, amount, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_list)
|
# File 'lib/factory_bot/syntax/methods.rb', line 62
|
#build_stubbed_pair(name, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_pair)
|
# 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.
|
# File 'lib/factory_bot/syntax/methods.rb', line 39
|
#create_list(name, amount, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_list)
|
# File 'lib/factory_bot/syntax/methods.rb', line 58
|
#create_pair(name, *traits_and_overrides, &block) ⇒ Array
(see #strategy_method_pair)
|
# File 'lib/factory_bot/syntax/methods.rb', line 74
|
#generate(*uri_parts, scope: nil) ⇒ Object
Generates and returns the next value in a global or factory sequence.
Arguments:
context: (Array of Symbols)
The definition context of the sequence, with the sequence name
as the final entry
scope: (object)(optional)
The object the sequence should be evaluated within
Returns:
The next value in the sequence. (Object)
Example:
generate(:my_factory, :my_trair, :my_sequence)
120 121 122 123 124 125 126 127 |
# File 'lib/factory_bot/syntax/methods.rb', line 120 def generate(*uri_parts, scope: nil) uri = FactoryBot::UriManager.build_uri(uri_parts) sequence = Sequence.find_by_uri(uri) || raise(KeyError, "Sequence not registered: #{FactoryBot::UriManager.build_uri(uri_parts)}") increment_sequence(uri, sequence, scope: scope) end |
#generate_list(*uri_parts, count, scope: nil) ⇒ Object
Generates and returns the list of values in a global or factory sequence.
Arguments:
uri_parts: (Array of Symbols)
The definition context of the sequence, with the sequence name
as the final entry
scope: (object)(optional)
The object the sequence should be evaluated within
Returns:
The next value in the sequence. (Object)
Example:
generate_list(:my_factory, :my_trair, :my_sequence, 5)
144 145 146 147 148 149 150 151 152 |
# File 'lib/factory_bot/syntax/methods.rb', line 144 def generate_list(*uri_parts, count, scope: nil) uri = FactoryBot::UriManager.build_uri(uri_parts) sequence = Sequence.find_by_uri(uri) || raise(KeyError, "Sequence not registered: '#{uri}'") (1..count).map do increment_sequence(uri, sequence, scope: scope) end end |