Class Factory
In: lib/factory_girl/aliases.rb
lib/factory_girl/attribute.rb
lib/factory_girl/syntax.rb
lib/factory_girl/sequence.rb
lib/factory_girl/proxy/attributes_for.rb
lib/factory_girl/proxy/create.rb
lib/factory_girl/proxy/build.rb
lib/factory_girl/proxy/stub.rb
lib/factory_girl/factory.rb
lib/factory_girl/proxy.rb
lib/factory_girl/attribute/callback.rb
lib/factory_girl/attribute/static.rb
lib/factory_girl/attribute/association.rb
lib/factory_girl/attribute/dynamic.rb
lib/factory_girl/syntax/make.rb
lib/factory_girl/syntax/sham.rb
lib/factory_girl/syntax/generate.rb
lib/factory_girl/syntax/blueprint.rb
lib/factory_girl.rb
Parent: Object

Methods

Classes and Modules

Module Factory::Syntax
Class Factory::AssociationDefinitionError
Class Factory::AttributeDefinitionError
Class Factory::DuplicateDefinitionError
Class Factory::InvalidCallbackNameError
Class Factory::Sequence
Class Factory::SequenceAbuseError

Constants

VERSION = "1.3.2"

Attributes

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.

Public Class methods

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 substituted 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)

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.

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.

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.

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.

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.

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)

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:

  • 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.

Public Instance methods

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:

  • 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.

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.
    

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.

[Validate]