Module: FactoryGirl::Syntax::Methods
- Included in:
- Default
- Defined in:
- lib/factory_girl/syntax/methods.rb
Instance Method Summary collapse
-
#attributes_for(name, overrides = {}, &block) ⇒ Object
Generates and returns a Hash of attributes from this factory.
-
#build(name, overrides = {}, &block) ⇒ Object
Generates and returns an instance from this factory.
-
#build_list(name, amount, overrides = {}) ⇒ Object
Builds and returns multiple instances from this factory as an array.
-
#build_stubbed(name, overrides = {}, &block) ⇒ Object
Generates and returns an object with all attributes from this factory stubbed out.
-
#create(name, overrides = {}, &block) ⇒ Object
Generates, saves, and returns an instance from this factory.
-
#create_list(name, amount, overrides = {}) ⇒ Object
Creates and returns multiple instances from this factory as an array.
-
#generate(name) ⇒ Object
Generates and returns the next value in a sequence.
Instance Method Details
#attributes_for(name, overrides = {}, &block) ⇒ Object
Generates and returns a Hash of attributes from this factory. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Arguments:
-
name:
Symbol
orString
The name of the factory that should be used. -
overrides:
Hash
Attributes to overwrite for this set. -
block: Yields the hash of attributes.
Returns: Hash
A set of attributes that can be used to build an instance of the class this factory generates.
19 20 21 |
# File 'lib/factory_girl/syntax/methods.rb', line 19 def attributes_for(name, overrides = {}, &block) FactoryGirl.factory_by_name(name).run(Proxy::AttributesFor, overrides, &block) end |
#build(name, overrides = {}, &block) ⇒ Object
Generates and returns an instance from this factory. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Arguments:
-
name:
Symbol
orString
The name of the factory that should be used. -
overrides:
Hash
Attributes to overwrite for this instance. -
block: Yields the built instance.
Returns: Object
An instance of the class this factory generates, with generated attributes assigned.
37 38 39 |
# File 'lib/factory_girl/syntax/methods.rb', line 37 def build(name, overrides = {}, &block) FactoryGirl.factory_by_name(name).run(Proxy::Build, overrides, &block) end |
#build_list(name, amount, overrides = {}) ⇒ Object
Builds and returns multiple instances from this factory as an array. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Arguments:
-
name:
Symbol
orString
The name of the factory to be used. -
amount:
Integer
number of instances to be built. -
overrides:
Hash
Attributes to overwrite for each instance.
Returns: Array
An array of instances of the class this factory generates, with generated attributes assigned.
95 96 97 |
# File 'lib/factory_girl/syntax/methods.rb', line 95 def build_list(name, amount, overrides = {}) amount.times.map { build(name, overrides) } end |
#build_stubbed(name, overrides = {}, &block) ⇒ Object
Generates and returns an object with all attributes from this factory stubbed out. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Arguments:
-
name:
Symbol
orString
The name of the factory that should be used. -
overrides:
Hash
Attributes to overwrite for this instance. -
block Yields the stubbed object.
Returns: Object
An object with generated attributes stubbed out.
77 78 79 |
# File 'lib/factory_girl/syntax/methods.rb', line 77 def build_stubbed(name, overrides = {}, &block) FactoryGirl.factory_by_name(name).run(Proxy::Stub, overrides, &block) end |
#create(name, overrides = {}, &block) ⇒ Object
Generates, saves, and returns an instance from this factory. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Instances are saved using the save!
method, so ActiveRecord models will raise ActiveRecord::RecordInvalid exceptions for invalid attribute sets.
Arguments:
-
name:
Symbol
orString
The name of the factory that should be used. -
overrides:
Hash
Attributes to overwrite for this instance. -
block: Yields the created instance.
Returns: Object
A saved instance of the class this factory generates, with generated attributes assigned.
59 60 61 |
# File 'lib/factory_girl/syntax/methods.rb', line 59 def create(name, overrides = {}, &block) FactoryGirl.factory_by_name(name).run(Proxy::Create, overrides, &block) end |
#create_list(name, amount, overrides = {}) ⇒ Object
Creates and returns multiple instances from this factory as an array. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Arguments:
-
name:
Symbol
orString
The name of the factory to be used. -
amount:
Integer
number of instances to be created. -
overrides:
Hash
Attributes to overwrite for each instance.
Returns: Array
An array of instances of the class this factory generates, with generated attributes assigned.
113 114 115 |
# File 'lib/factory_girl/syntax/methods.rb', line 113 def create_list(name, amount, overrides = {}) amount.times.map { create(name, overrides) } end |
#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)
125 126 127 |
# File 'lib/factory_girl/syntax/methods.rb', line 125 def generate(name) FactoryGirl.sequence_by_name(name).next end |