Forked to add patches, see branches for more info
Pull request Compare This branch is 745 commits behind thoughtbot:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



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




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

Defining factories

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

# 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

# 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

It is highly recommended that you have one factory for each class that provides the simplest set of attributes necessary to create an instance of that class. If you're creating ActiveRecord objects, that means that you should only provide attributes that are required through validations and that do not have defaults. Other factories can be created through inheritance to cover common scenarios for each class.

Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:


Using factories

factory_girl supports several different build strategies: build, create, attributes_for and stub:

# Returns a User instance that's not saved
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 an object with all defined attributes stubbed out:
stub = Factory.stub(:user)

You can use the Factory method as a shortcut for the default build strategy:

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

The default strategy can be overriden:

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

user = Factory(:user)

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

# Build a User instance and override the first_name property
user =, :first_name => 'Joe')
# => "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 }

Dependent Attributes

Attributes can be based on the values of other attributes using the proxy that is yieled 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') }

The behavior of the association method varies depending on the build strategy used for the parent object.

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

# Builds and saves a User, and then builds but does not save a Post
post.new_record?       # => true # => false

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

# 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.


You can easily create multiple factories for the same class without repeating common attributes by using inheritance:

Factory.define :post do |p|
  # the 'title' attribute is required for all posts
  p.title 'A title'

Factory.define :approved_post, :parent => :post do |p|
  p.approved true
  # the 'approver' association is required for an approved post
  p.association :approver, :factory => :user


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
# => ""

Sequences can be used in lazy attributes:

Factory.define :user do |f| { }

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(:email) {|n| "person#{n}" }

Alternate Syntaxes

Users' tastes for syntax vary dramatically, but most users are looking for a common feature set. Because of this, factory_girl supports “syntax layers” which provide alternate interfaces. See Factory::Syntax for information about the various layers available.

More Information

Our blog:

factory_girl rdoc:

Mailing list:

factory_girl issues:


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


factory_girl was written by Joe Ferris with contributions from several authors, including:

  • Alex Sharp

  • Eugene Bolshakov

  • Jon Yurek

  • Josh Nichols

  • Josh Owens

The syntax layers are derived from software written by the following authors:

  • Pete Yandell

  • Rick Bradley

  • Yossef Mendelssohn

Thanks to all members of thoughtbot for inspiration, ideas, and funding.

Copyright 2008-2009 Joe Ferris and thoughtbot, inc.