class FactoryGirl::DefinitionProxy

Constants

UNPROXIED_METHODS

Attributes

child_factories[R]

Public Class Methods

new(definition, ignore = false) click to toggle source
# File lib/factory_girl/definition_proxy.rb, line 11
def initialize(definition, ignore = false)
  @definition      = definition
  @ignore          = ignore
  @child_factories = []
end

Public Instance Methods

add_attribute(name, value = nil, &block) click to toggle source

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 FactoryGirl::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/definition_proxy.rb, line 35
def add_attribute(name, value = nil, &block)
  raise AttributeDefinitionError, "Both value and block given" if value && block_given?

  declaration = if block_given?
    Declaration::Dynamic.new(name, @ignore, block)
  else
    Declaration::Static.new(name, value, @ignore)
  end

  @definition.declare_attribute(declaration)
end
after_build(&block) click to toggle source
# File lib/factory_girl/definition_proxy.rb, line 141
def after_build(&block)
  @definition.add_callback(Callback.new(:after_build, block))
end
after_create(&block) click to toggle source
# File lib/factory_girl/definition_proxy.rb, line 145
def after_create(&block)
  @definition.add_callback(Callback.new(:after_create, block))
end
after_stub(&block) click to toggle source
# File lib/factory_girl/definition_proxy.rb, line 149
def after_stub(&block)
  @definition.add_callback(Callback.new(:after_stub, block))
end
association(name, options = {}) click to toggle source

Adds an attribute that builds an association. The associated instance will be built using the same build strategy as the parent instance.

Example:

factory :user do
  name 'Joey'
end

factory :post do
  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/definition_proxy.rb, line 137
def association(name, options = {})
  @definition.declare_attribute(Declaration::Association.new(name, options))
end
factory(name, options = {}, &block) click to toggle source
# File lib/factory_girl/definition_proxy.rb, line 157
def factory(name, options = {}, &block)
  @child_factories << [name, options, block]
end
ignore(&block) click to toggle source
# File lib/factory_girl/definition_proxy.rb, line 47
def ignore(&block)
  proxy = DefinitionProxy.new(@definition, true)
  proxy.instance_eval(&block)
end
method_missing(name, *args, &block) click to toggle source

Calls #add_attribute using the missing method name as the name of the attribute, so that:

factory :user do
  name 'Billy Idol'
end

and:

factory :user do
  add_attribute :name, 'Billy Idol'
end

are equivalent.

If no argument or block is given, factory_girl will look for a sequence or association with the same name. This means that:

factory :user do
  email { create(:email) }
  association :account
end

and:

factory :user do
  email
  account
end

are equivalent.

# File lib/factory_girl/definition_proxy.rb, line 83
def method_missing(name, *args, &block)
  if args.empty? && block.nil?
    @definition.declare_attribute(Declaration::Implicit.new(name, @definition, @ignore))
  elsif args.first.is_a?(Hash) && args.first.has_key?(:factory)
    association(name, *args)
  else
    add_attribute(name, *args, &block)
  end
end
sequence(name, start_value = 1, &block) click to toggle source

Adds an attribute that will have unique values generated by a sequence with a specified format.

The result of:

factory :user do
  sequence(:email) { |n| "person#{n}@example.com" }
end

Is equal to:

sequence(:email) { |n| "person#{n}@example.com" }

factory :user do
  email { FactoryGirl.create(:email) }
end

Except that no globally available sequence will be defined.

# File lib/factory_girl/definition_proxy.rb, line 109
def sequence(name, start_value = 1, &block)
  sequence = Sequence.new(name, start_value, &block)
  add_attribute(name) { sequence.next }
end
to_create(&block) click to toggle source
# File lib/factory_girl/definition_proxy.rb, line 153
def to_create(&block)
  @definition.to_create(&block)
end
trait(name, &block) click to toggle source
# File lib/factory_girl/definition_proxy.rb, line 161
def trait(name, &block)
  @definition.define_trait(Trait.new(name, &block))
end