Module: FactoryGirl::Syntax::Methods

Included in:
Default
Defined in:
lib/factory_girl/syntax/methods.rb

Instance Method Summary collapse

Instance Method Details

#attributes_for(name, *traits_and_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.

DEPRECATED

Use build or create instead, calling attributes on the result

Arguments:

  • name: Symbol or String The name of the factory that should be used.

  • traits_and_overrides: Array

    *Array

    Traits to be applied

    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.



24
25
26
27
# File 'lib/factory_girl/syntax/methods.rb', line 24

def attributes_for(name, *traits_and_overrides, &block)
  $stderr.puts "DEPRECATION WARNING: FactoryGirl.attributes_for is deprecated; use FactoryGirl.build or FactoryGirl.create and call #attributes on the result instead."
  run_factory_girl_proxy(name, traits_and_overrides, Proxy::AttributesFor, &block)
end

#build(name, *traits_and_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 or String The name of the factory that should be used.

  • traits_and_overrides: Array

    *Array

    Traits to be applied

    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.



44
45
46
# File 'lib/factory_girl/syntax/methods.rb', line 44

def build(name, *traits_and_overrides, &block)
  run_factory_girl_proxy(name, traits_and_overrides, Proxy::Build, &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 or String 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.



109
110
111
# File 'lib/factory_girl/syntax/methods.rb', line 109

def build_list(name, amount, overrides = {})
  amount.times.map { build(name, overrides) }
end

#build_stubbed(name, *traits_and_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.

DEPRECATED

Use build instead

Arguments:

  • name: Symbol or String The name of the factory that should be used.

  • traits_and_overrides: Array

    *Array

    Traits to be applied

    Hash

    Attributes to overwrite for this instance.

  • block Yields the stubbed object.

Returns: Object An object with generated attributes stubbed out.



90
91
92
93
# File 'lib/factory_girl/syntax/methods.rb', line 90

def build_stubbed(name, *traits_and_overrides, &block)
  $stderr.puts "DEPRECATION WARNING: FactoryGirl.build_stubbed is deprecated; use FactoryGirl.build instead."
  run_factory_girl_proxy(name, traits_and_overrides, Proxy::Stub, &block)
end

#create(name, *traits_and_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 or String The name of the factory that should be used.

  • traits_and_overrides: Array

    *Array

    Traits to be applied

    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.



67
68
69
# File 'lib/factory_girl/syntax/methods.rb', line 67

def create(name, *traits_and_overrides, &block)
  run_factory_girl_proxy(name, traits_and_overrides, Proxy::Create, &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 or String 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.



127
128
129
# File 'lib/factory_girl/syntax/methods.rb', line 127

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)


139
140
141
# File 'lib/factory_girl/syntax/methods.rb', line 139

def generate(name)
  FactoryGirl.sequence_by_name(name).next
end