public
Rubygem
Fork of thoughtbot/factory_girl
Clone URL: git://github.com/ihower/factory_girl.git
name age message
file .autotest Sun Jun 01 11:14:21 -0700 2008 Added autotest mappings [Joe Ferris]
file .gitignore Wed May 28 15:28:02 -0700 2008 Added a schema to test with [Joe Ferris]
file LICENSE Sun Jun 01 11:27:59 -0700 2008 Added a license file [Joe Ferris]
file README.textile Sat Jun 14 14:07:43 -0700 2008 Add Factory.lorem [ihower]
file Rakefile Fri Jun 06 08:43:27 -0700 2008 Renamed README to README.textile [Joe Ferris]
file factory_girl.gemspec Fri Jun 06 08:43:27 -0700 2008 Renamed README to README.textile [Joe Ferris]
directory lib/ Tue Jun 17 01:18:06 -0700 2008 fixed lorem require err [ihower]
directory test/ Tue Jun 10 15:57:06 -0700 2008 Allowed 'name' attributes to be used for factories [Chris O'Sullivan]
README.textile

factory_girl

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

Defining factories

# 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
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 }
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 (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
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

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"

Using factories

# Build and save a User instance
Factory(:user)

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

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

New features in this fork branch

HashFactory(:user) is a shoutcut for Factory.attributes_for

Factory.define :post do |p|
  # ...
  p.description {|p| Factory.lorem }
end

More Information

Our blog

factory_girl rdoc