Defines a new alias for attributes.
Arguments:
pattern: Regexp A pattern that will be matched against attributes when looking for aliases. Contents captured in the pattern can be used in the alias.
replace: String The alias that results from the matched pattern. Captured strings can be substituded like with +String#sub+.
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)
# File lib/factory_girl/aliases.rb, line 35 def self.alias (pattern, replace) self.aliases << [pattern, replace] end
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 or String The name of the factory that should be used.
overrides: Hash Attributes to overwrite for this set.
Returns: Hash A set of attributes that can be used to build an instance of the class this factory generates.
# File lib/factory_girl/factory.rb, line 233 def self.attributes_for (name, overrides = {}) factory_by_name(name).run(Proxy::AttributesFor, overrides) end
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 or String The name of the factory that should be used.
overrides: Hash Attributes to overwrite for this instance.
Returns: Object An instance of the class this factory generates, with generated attributes assigned.
# File lib/factory_girl/factory.rb, line 249 def self.build (name, overrides = {}) factory_by_name(name).run(Proxy::Build, overrides) end
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 or String The name of the factory that should be used.
overrides: Hash Attributes to overwrite for this instance.
Returns: Object A saved instance of the class this factory generates, with generated attributes assigned.
# File lib/factory_girl/factory.rb, line 269 def self.create (name, overrides = {}) factory_by_name(name).run(Proxy::Create, overrides) end
Executes the default strategy for the given factory. This is usually create, but it can be overridden for each factory.
Arguments:
name: Symbol or String The name of the factory that should be used.
overrides: Hash Attributes to overwrite for this instance.
Returns: Object The result of the default strategy.
# File lib/factory_girl/factory.rb, line 300 def self.default_strategy (name, overrides = {}) self.send(factory_by_name(name).default_strategy, name, overrides) end
Defines a new factory that can be used by the build strategies (create and build) to build new objects.
Arguments:
name: Symbol or String A unique name used to identify this factory.
options: Hash
Options:
class: Symbol, Class, or String The class that will be used when generating instances for this factory. If not specified, the class will be guessed from the factory name.
parent: Symbol The parent factory. If specified, the attributes from the parent factory will be copied to the current one with an ability to override them.
default_strategy: Symbol The strategy that will be used by the Factory shortcut method. Defaults to :create.
Yields: Factory The newly created factory.
# File lib/factory_girl/factory.rb, line 54 def self.define (name, options = {}) instance = Factory.new(name, options) yield(instance) if parent = options.delete(:parent) instance.inherit_from(Factory.factory_by_name(parent)) end if self.factories[instance.factory_name] raise DuplicateDefinitionError, "Factory already defined: #{name}" end self.factories[instance.factory_name] = instance end
# File lib/factory_girl/factory.rb, line 329 def self.factory_by_name (name) factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}") end
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)
# File lib/factory_girl/sequence.rb, line 55 def self.next (sequence) unless self.sequences.key?(sequence) raise "No such sequence: #{sequence}" end self.sequences[sequence].next end
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" }
# File lib/factory_girl/sequence.rb, line 43 def self.sequence (name, &block) self.sequences[name] = Sequence.new(&block) end
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 or String The name of the factory that should be used.
overrides: Hash Attributes to overwrite for this instance.
Returns: Object An object with generated attributes stubbed out.
# File lib/factory_girl/factory.rb, line 285 def self.stub (name, overrides = {}) factory_by_name(name).run(Proxy::Stub, overrides) end
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 overriden 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:
name: Symbol or String The name of this attribute. This will be assigned using :“#{name}=” for generated instances.
value: Object If no block is given, this value will be used for this attribute.
# File lib/factory_girl/factory.rb, line 113 def add_attribute (name, value = nil, &block) if block_given? if value raise AttributeDefinitionError, "Both value and block given" else attribute = Attribute::Dynamic.new(name, block) end else attribute = Attribute::Static.new(name, value) end if attribute_defined?(attribute.name) raise AttributeDefinitionError, "Attribute already defined: #{name}" end @attributes << attribute end
# File lib/factory_girl/factory.rb, line 201 def after_build(&block) callback(:after_build, &block) end
# File lib/factory_girl/factory.rb, line 205 def after_create(&block) callback(:after_create, &block) end
# File lib/factory_girl/factory.rb, line 209 def after_stub(&block) callback(:after_stub, &block) end
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:
name: Symbol The name of this attribute.
options: Hash
Options:
factory: Symbol or String
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.
# File lib/factory_girl/factory.rb, line 172 def association (name, options = {}) factory_name = options.delete(:factory) || name if factory_name_for(factory_name) == self.factory_name raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.factory_name}'" end @attributes << Attribute::Association.new(name, factory_name, options) end
# File lib/factory_girl/factory.rb, line 341 def associations attributes.select {|attribute| attribute.is_a?(Attribute::Association) } end
# File lib/factory_girl/factory.rb, line 213 def callback(name, &block) unless [:after_build, :after_create, :after_stub].include?(name.to_sym) raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :after_build, :after_create, and :after_stub" end @attributes << Attribute::Callback.new(name.to_sym, block) end
# File lib/factory_girl/factory.rb, line 333 def human_name(*args, &block) if args.size == 0 && block.nil? factory_name.to_s.gsub('_', ' ') else add_attribute(:human_name, *args, &block) end end
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.
# File lib/factory_girl/factory.rb, line 145 def method_missing (name, *args, &block) add_attribute(name, *args, &block) end
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.
# File lib/factory_girl/factory.rb, line 196 def sequence (name, &block) s = Sequence.new(&block) add_attribute(name) { s.next } end
Generated with the Darkfish Rdoc Generator 2.