Class: Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_girl/proxy.rb,
lib/factory_girl/syntax.rb,
lib/factory_girl/aliases.rb,
lib/factory_girl/factory.rb,
lib/factory_girl/sequence.rb,
lib/factory_girl/attribute.rb,
lib/factory_girl/proxy/stub.rb,
lib/factory_girl/proxy/build.rb,
lib/factory_girl/syntax/make.rb,
lib/factory_girl/syntax/sham.rb,
lib/factory_girl/proxy/create.rb,
lib/factory_girl/syntax/generate.rb,
lib/factory_girl/attribute/static.rb,
lib/factory_girl/syntax/blueprint.rb,
lib/factory_girl/attribute/dynamic.rb,
lib/factory_girl/proxy/attributes_for.rb,
lib/factory_girl/attribute/association.rb

Defined Under Namespace

Modules: Syntax Classes: AssociationDefinitionError, Attribute, AttributeDefinitionError, Proxy, Sequence

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Factory

:nodoc:



65
66
67
68
69
70
# File 'lib/factory_girl/factory.rb', line 65

def initialize (name, options = {}) #:nodoc:
  assert_valid_options(options)
  @factory_name = factory_name_for(name)
  @options      = options      
  @attributes   = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

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.



131
132
133
# File 'lib/factory_girl/factory.rb', line 131

def method_missing (name, *args, &block)
  add_attribute(name, *args, &block)
end

Class Attribute Details

.aliasesObject

:nodoc:



4
5
6
# File 'lib/factory_girl/aliases.rb', line 4

def aliases
  @aliases
end

.definition_file_pathsObject

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.



14
15
16
# File 'lib/factory_girl/factory.rb', line 14

def definition_file_paths
  @definition_file_paths
end

.factoriesObject

:nodoc:



8
9
10
# File 'lib/factory_girl/factory.rb', line 8

def factories
  @factories
end

.sequencesObject

:nodoc:



21
22
23
# File 'lib/factory_girl/sequence.rb', line 21

def sequences
  @sequences
end

Instance Attribute Details

#attributesObject (readonly)

:nodoc:



21
22
23
# File 'lib/factory_girl/factory.rb', line 21

def attributes
  @attributes
end

#factory_nameObject (readonly)

:nodoc:



20
21
22
# File 'lib/factory_girl/factory.rb', line 20

def factory_name
  @factory_name
end

Class Method Details

.alias(pattern, replace) ⇒ Object

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)


35
36
37
# File 'lib/factory_girl/aliases.rb', line 35

def self.alias (pattern, replace)
  self.aliases << [pattern, replace]
end

.aliases_for(attribute) ⇒ Object

:nodoc:



39
40
41
42
43
44
45
46
47
48
# File 'lib/factory_girl/aliases.rb', line 39

def self.aliases_for (attribute) #:nodoc:
  aliases.collect do |params|
    pattern, replace = *params
    if pattern.match(attribute.to_s)
      attribute.to_s.sub(pattern, replace).to_sym
    else
      nil
    end
  end.compact << attribute
end

.attributes_for(name, overrides = {}) ⇒ 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.

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.



200
201
202
# File 'lib/factory_girl/factory.rb', line 200

def self.attributes_for (name, overrides = {})
  factory_by_name(name).run(Proxy::AttributesFor, overrides)
end

.build(name, overrides = {}) ⇒ 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.

  • overrides: Hash Attributes to overwrite for this instance.

Returns: Object An instance of the class this factory generates, with generated attributes assigned.



216
217
218
# File 'lib/factory_girl/factory.rb', line 216

def self.build (name, overrides = {})
  factory_by_name(name).run(Proxy::Build, overrides)
end

.create(name, overrides = {}) ⇒ 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.

  • overrides: Hash Attributes to overwrite for this instance.

Returns: Object A saved instance of the class this factory generates, with generated attributes assigned.



236
237
238
# File 'lib/factory_girl/factory.rb', line 236

def self.create (name, overrides = {})
  factory_by_name(name).run(Proxy::Create, overrides)
end

.default_strategy(name, overrides = {}) ⇒ Object

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.



267
268
269
# File 'lib/factory_girl/factory.rb', line 267

def self.default_strategy (name, overrides = {})  
  self.send(factory_by_name(name).default_strategy, name, overrides)
end

.define(name, options = {}) {|instance| ... } ⇒ Object

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.

Yields:

  • (instance)


44
45
46
47
48
49
50
51
# File 'lib/factory_girl/factory.rb', line 44

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    
  self.factories[instance.factory_name] = instance
end

.find_definitionsObject

:nodoc:



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/factory_girl/factory.rb', line 271

def self.find_definitions #:nodoc:
  definition_file_paths.each do |path|
    require("#{path}.rb") if File.exists?("#{path}.rb")

    if File.directory? path
      Dir[File.join(path, '*.rb')].each do |file|
        require file
      end
    end
  end
end

.next(sequence) ⇒ 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)


52
53
54
55
56
57
58
# File 'lib/factory_girl/sequence.rb', line 52

def self.next (sequence)
  unless self.sequences.key?(sequence)
    raise "No such sequence: #{sequence}"
  end

  self.sequences[sequence].next
end

.sequence(name, &block) ⇒ 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" }


40
41
42
# File 'lib/factory_girl/sequence.rb', line 40

def self.sequence (name, &block)
  self.sequences[name] = Sequence.new(&block)
end

.stub(name, overrides = {}) ⇒ 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.

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.



252
253
254
# File 'lib/factory_girl/factory.rb', line 252

def self.stub (name, overrides = {})
  factory_by_name(name).run(Proxy::Stub, overrides)
end

Instance Method Details

#add_attribute(name, value = nil, &block) ⇒ Object

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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/factory_girl/factory.rb', line 99

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

#association(name, options = {}) ⇒ Object

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.
    


158
159
160
161
162
163
164
# File 'lib/factory_girl/factory.rb', line 158

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

#build_classObject

:nodoc:



57
58
59
# File 'lib/factory_girl/factory.rb', line 57

def build_class #:nodoc:
  @build_class ||= class_for(class_name)
end

#class_nameObject

:nodoc:



53
54
55
# File 'lib/factory_girl/factory.rb', line 53

def class_name #:nodoc:
  @options[:class] || factory_name
end

#default_strategyObject

:nodoc:



61
62
63
# File 'lib/factory_girl/factory.rb', line 61

def default_strategy #:nodoc:
  @options[:default_strategy] || :create
end

#inherit_from(parent) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
# File 'lib/factory_girl/factory.rb', line 72

def inherit_from(parent) #:nodoc:
  @options[:class] ||= parent.class_name
  parent.attributes.each do |attribute|
    unless attribute_defined?(attribute.name)
      @attributes << attribute.clone
    end
  end
end

#run(proxy_class, overrides) ⇒ Object

:nodoc:



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/factory_girl/factory.rb', line 283

def run (proxy_class, overrides) #:nodoc:
  proxy = proxy_class.new(build_class)
  overrides = symbolize_keys(overrides)
  overrides.each {|attr, val| proxy.set(attr, val) }
  passed_keys = overrides.keys.collect {|k| Factory.aliases_for(k) }.flatten
  @attributes.each do |attribute|
    unless passed_keys.include?(attribute.name)
      attribute.add_to(proxy)
    end
  end
  proxy.result
end

#sequence(name, &block) ⇒ Object

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.



182
183
184
185
# File 'lib/factory_girl/factory.rb', line 182

def sequence (name, &block)
  s = Sequence.new(&block)
  add_attribute(name) { s.next }
end