Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
152 lines (101 sloc) 4.18 KB


Written by Joe Ferris.

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

Copyright 2008 Joe Ferris and thoughtbot, inc.


Github: Page Clone


gem install thoughtbot-factory_girl —source

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 => “


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

Defining factories

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

# 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

It is recommended that you create a test/factories.rb file and define your
factories there. This file can be included from test_helper or directly from
your test files. Don’t forget:

require 'factory_girl'

Lazy Attributes

Most 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 }

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' {|a| "#{a.first_name}.#{a.last_name}".downcase }

Factory(:user, :last_name => 'Doe').email
# => ""


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

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

When using the association method, the same build strategy (build, create, or attributes_for) will be used for all generated instances:

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

# Builds but does not save a User and a Post
post.new_record?       # => true # => true

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

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

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

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


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

# Defines a new sequence
Factory.sequence :email do |n|
end :email
# => "" :email
# => ""

Using factories

# Build and save a User instance

# Build a User instance and override the first_name property, :first_name => 'Joe')

# Return an attributes Hash that can be used to build a User instance
attrs = Factory.attributes_for(:user)

More Information

Our blog

factory_girl rdoc