VERSION | = | "1.3.2" |
definition_file_paths | [RW] | An Array of strings specifying locations that should be searched for factory definitions. By default, factory_girl will attempt to require "factories," "test/factories," and "spec/factories." Only the first existing file will be loaded. |
Defines a new alias for attributes.
Arguments:
Example:
Factory.alias /(.*)_confirmation/, '\1'
factory_girl starts with aliases for foreign keys, so that a :user association can be overridden by a :user_id parameter:
Factory.define :post do |p| p.association :user end # The user association will not be built in this example. The user_id # will be used instead. Factory(:post, :user_id => 1)
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:
Returns: Hash A set of attributes that can be used to build an instance of the class this factory generates.
Generates and returns an instance from this factory. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Arguments:
Returns: Object An instance of the class this factory generates, with generated attributes assigned.
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:
Returns: Object A saved instance of the class this factory generates, with generated attributes assigned.
Executes the default strategy for the given factory. This is usually create, but it can be overridden for each factory.
Arguments:
Returns: Object The result of the default strategy.
Defines a new factory that can be used by the build strategies (create and build) to build new objects.
Arguments:
Options:
Yields: Factory The newly created factory.
Defines a new sequence that can be used to generate unique values in a specific format.
Arguments:
name: (Symbol) A unique name for this sequence. This name will be referenced when calling next to generate new values from this sequence. block: (Proc) The code to generate each value in the sequence. This block will be called with a unique number each time a value in the sequence is to be generated. The block should return the generated value for the sequence.
Example:
Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
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:
Returns: Object An object with generated attributes stubbed out.
Adds an attribute that should be assigned on generated instances for this factory.
This method should be called with either a value or block, but not both. If called with a block, the attribute will be generated "lazily," whenever an instance is generated. Lazy attribute blocks will not be called if that attribute is overridden for a specific instance.
When defining lazy attributes, an instance of Factory::Proxy will be yielded, allowing associations to be built using the correct build strategy.
Arguments:
Adds an attribute that builds an association. The associated instance will be built using the same build strategy as the parent instance.
Example:
Factory.define :user do |f| f.name 'Joey' end Factory.define :post do |f| f.association :author, :factory => :user end
Arguments:
Options:
The name of the factory to use when building the associated instance. If no name is given, the name of the attribute is assumed to be the name of the factory. For example, a "user" association will by default use the "user" factory.
Calls add_attribute using the missing method name as the name of the attribute, so that:
Factory.define :user do |f| f.name 'Billy Idol' end
and:
Factory.define :user do |f| f.add_attribute :name, 'Billy Idol' end
are equivilent.
Adds an attribute that will have unique values generated by a sequence with a specified format.
The result of:
Factory.define :user do |f| f.sequence(:email) { |n| "person#{n}@example.com" } end
Is equal to:
Factory.sequence(:email) { |n| "person#{n}@example.com" } Factory.define :user do |f| f.email { Factory.next(:email) } end
Except that no globally available sequence will be defined.