jwhitmire / factory_girl forked from thoughtbot/factory_girl

This URL has Read+Write access

name age message
file .autotest Sun Jun 01 11:14:21 -0700 2008 Added autotest mappings [jferris]
file .gitignore Fri Nov 28 11:48:13 -0800 2008 Added rcov to the mix. [technicalpickles]
file CONTRIBUTION_GUIDELINES.rdoc Fri Nov 28 14:22:50 -0800 2008 Added contribution guidelines [jferris]
file Changelog Fri Nov 28 14:03:53 -0800 2008 Updated the Changelog [jferris]
file LICENSE Sun Jun 01 11:27:59 -0700 2008 Added a license file [jferris]
file README.textile Loading commit data...
file Rakefile Thu Dec 11 12:54:33 -0800 2008 #19: removed ActiveSupport as a dependency [jferris]
file factory_girl.gemspec Thu Dec 11 12:54:33 -0800 2008 #19: removed ActiveSupport as a dependency [jferris]
directory lib/
directory test/
README.textile

factory_girl

Factory Girl is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and mock objects), and support for multiple factories for the same class (user, admin_user, and so on).

Written by Joe Ferris.

Thanks to Tammer Saleh, Dan Croak, and Jon Yurek of thoughtbot, inc.

Copyright 2008 Joe Ferris and thoughtbot, inc.

Download

Github: Page Clone

Gem:

gem install thoughtbot-factory_girl —source http://gems.github.com

Note: if you install factory_girl using the gem from Github, you’ll need this
in your environment.rb if you want to use Rails 2.1+’s dependency manager:

config.gem “thoughtbot-factory_girl”,
:lib => “factory_girl”,
:source => “http://gems.github.com”

Defining factories

Each factory has a name and a bunch of attributes. The name is used to guess the class of the object by default, but it’s possible to excplicitly specify it:

# This will guess the User class
Factory.define :user do |u|
  u.first_name 'John'
  u.last_name  'Doe'
  u.admin false
end

# This will use the User class (Admin would have been guessed)
Factory.define :admin, :class => User do |u|
  u.first_name 'Admin'
  u.last_name  'User'
  u.admin true
end

# The same, but using a string instead of class constant
Factory.define :admin, :class => 'user' do |u|
  u.first_name 'Admin'
  u.last_name  'User'
  u.admin true
end

Factories can either be defined in test/factories.rb file or in separate files
under test/factories or test/spec

Using factories

Factroy girl supports several different build strategies: build, create, attribtues_for and stub:

# Returns a User instance that's not saved
user = Factory.build :user

#Returns a saved User instance
user = Factory.create :user

#Returns a hash of attributes that can be used to build a User instance:
attrs = Factory.attributes_for(:user)

#Returns a mock object with all defined attributes stubbed out:
mock = Factory.stub(:user)

The default is “create” and it’s used by the Factory shortcut method:

# Same as Factory.create :user:
user = Factory :user

The default strategy can be overriden with the :default_strategy_parameter though:

# Now same as Factory.build :user
Factory.define :user, :default_strategy => :build do |u|
  ....
end

user = Factory :user

No matter which startegy is used, it’s possible to override the attributes by passing a hash:

# Build a User instance and override the first_name property
user = Factory.build(:user, :first_name => 'Joe')
user.first_name
# => "Joe"

Lazy Attributes

Most factory attributes can be added using static values that are evaluated when the factory is defined, but some attributes (such as associations and other attributes that must be dynamically generated) will need values assigned each time an instance is generated. These “lazy” attributes can be added by passing a block instead of a parameter:

Factory.define :user do |u|
  # ...
  u.activation_code { User.generate_activation_code }
end

Dependent Attributes

Some attributes may need to be generated based on the values of other attributes. This can be done by calling the attribute name on Factory::AttributeProxy, which is yielded to lazy attribute blocks:

Factory.define :user do |u|
  u.first_name 'Joe'
  u.last_name  'Blow'
  u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
end

Factory(:user, :last_name => 'Doe').email
# => "joe.doe@example.com"

Associations

Associated instances can be generated by using the association method when
defining a lazy attribute:

Factory.define :post do |p|
  # ...
  p.author {|author| author.association(:user, :last_name => 'Writely') }
end

When using the association method, the same build strategy will be used for all generated instances:

# Builds and saves a User and a Post
post = Factory(:post)
post.new_record?       # => false
post.author.new_record # => false

# Builds but does not save a User and a Post
Factory.build(:post)
post.new_record?       # => true
post.author.new_record # => true

Because this pattern is so common, a prettier syntax is available for defining
associations:

# The following definitions are equivilent:
Factory.define :post do |p|
  p.author {|a| a.association(:user) }
end

Factory.define :post do |p|
  p.association :author, :factory => :user
end

If the factory name is the same as the association name, the factory name can
be left out.

Inheritance

In case there are several factories for the same class that share common attributes, you can define them in a parent factory and inherit other ones from it. A child factory can override parent attributes and define new ones:


Factory.define :user do |u|
  u.first_name 'John'
  u.last_name  'Doe'
  u.admin false
end

Factory.define :admin, :parent => :user do |u|
  u.admin true
end

Factory.define :guest, :parent => :user do |u|
u.last_name ‘Anonymous’
u.guest true
end

Sequences

Unique values in a specific format (for example, e-mail addresses) can be
generated using sequences. Sequences are defined by calling Factory.sequence,
and values in a sequence are generated by calling Factory.next:

# Defines a new sequence
Factory.sequence :email do |n|
  "person#{n}@example.com"
end

Factory.next :email
# => "person1@example.com"

Factory.next :email
# => "person2@example.com"

Sequences can be used as lazy attributes:

Factory.define :user do |f|
f.email { Factory.next(:email) }
end

And it’s also possible to define an in-line sequence that is only used in
a particular factory:

Factory.define :user do |f|
f.sequence(:username) { |n| “person#{n}” }
end

More Information

Our blog

factory_girl rdoc

Mailing list

Contributing

Please read the contribution guidelines before submitting patches or pull requests.