2011-02-07 17:48:29 -05:00
Getting Started
===============
2011-07-04 09:49:09 -04:00
Update Your Gemfile
-------------------
2011-09-06 10:30:42 -04:00
If you're using Rails, you'll need to change the required version of `factory_girl_rails` :
2011-07-04 09:49:09 -04:00
2012-01-13 11:44:27 -05:00
```ruby
2012-03-31 11:27:39 -04:00
gem "factory_girl_rails", "~> 3.0"
2012-01-13 11:44:27 -05:00
```
2011-07-04 09:49:09 -04:00
If you're *not* using Rails, you'll just have to change the required version of `factory_girl` :
2012-01-13 11:44:27 -05:00
```ruby
2012-03-09 15:52:29 -05:00
gem "factory_girl", "~> 3.0"
2012-01-13 11:44:27 -05:00
```
2011-07-04 09:49:09 -04:00
Once your Gemfile is updated, you'll want to update your bundle.
2011-02-07 17:48:29 -05:00
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:
2012-01-13 11:44:27 -05:00
```ruby
# This will guess the User class
FactoryGirl.define do
factory :user do
2012-03-31 11:36:20 -04:00
first_name "John"
last_name "Doe"
2012-01-13 11:44:27 -05:00
admin false
end
# This will use the User class (Admin would have been guessed)
2012-03-31 11:36:20 -04:00
factory :admin, class: User do
first_name "Admin"
last_name "User"
admin true
2012-01-13 11:44:27 -05:00
end
end
```
2011-02-07 17:48:29 -05:00
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.
Attempting to define multiple factories with the same name will raise an error.
Factories can be defined anywhere, but will be automatically loaded if they
are defined in files at the following locations:
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb
Using factories
---------------
2011-09-16 09:27:05 -04:00
factory\_girl supports several different build strategies: build, create, attributes\_for and stub:
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
```ruby
# Returns a User instance that's not saved
user = FactoryGirl.build(:user)
2011-06-29 11:46:59 -04:00
2012-01-13 11:44:27 -05:00
# Returns a saved User instance
user = FactoryGirl.create(:user)
2011-06-29 11:46:59 -04:00
2012-01-13 11:44:27 -05:00
# Returns a hash of attributes that can be used to build a User instance
attrs = FactoryGirl.attributes_for(:user)
2011-06-29 11:46:59 -04:00
2012-01-13 11:44:27 -05:00
# Returns an object with all defined attributes stubbed out
stub = FactoryGirl.build_stubbed(:user)
2011-10-12 11:35:10 -04:00
2012-01-13 11:44:27 -05:00
# Passing a block to any of the methods above will yield the return object
FactoryGirl.create(:user) do |user|
user.posts.create(attributes_for(:post))
end
```
2011-02-07 17:48:29 -05:00
No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
2012-01-13 11:44:27 -05:00
```ruby
# Build a User instance and override the first_name property
2012-03-31 11:36:20 -04:00
user = FactoryGirl.build(:user, first_name: "Joe")
2012-01-13 11:44:27 -05:00
user.first_name
# => "Joe"
```
2011-02-07 17:48:29 -05:00
If repeating "FactoryGirl" is too verbose for you, you can mix the syntax methods in:
2012-01-13 11:44:27 -05:00
```ruby
# rspec
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
# Test::Unit
class Test::Unit::TestCase
2012-03-27 05:42:05 -04:00
include FactoryGirl::Syntax::Methods
2012-01-13 11:44:27 -05:00
end
2012-03-31 11:36:20 -04:00
# Cucumber
World(FactoryGirl::Syntax::Methods)
2012-01-13 11:44:27 -05:00
```
2011-02-07 17:48:29 -05:00
2011-07-01 11:07:02 -04:00
This would allow you to write:
2012-01-13 11:44:27 -05:00
```ruby
describe User, "#full_name" do
2012-03-31 11:36:20 -04:00
subject { create(:user, first_name: "John", last_name: "Doe") }
2011-07-01 11:07:02 -04:00
2012-01-13 11:44:27 -05:00
its(:full_name) { should == "John Doe" }
end
```
2011-07-01 11:07:02 -04:00
2011-02-07 17:48:29 -05:00
Lazy Attributes
---------------
2011-09-16 09:27:05 -04:00
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:
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
# ...
activation_code { User.generate_activation_code }
date_of_birth { 21.years.ago }
end
```
2011-02-07 17:48:29 -05:00
2011-07-01 11:14:27 -04:00
Aliases
-------
Aliases allow you to use named associations more easily.
2012-01-13 11:44:27 -05:00
```ruby
2012-03-31 11:36:20 -04:00
factory :user, aliases: [:author, :commenter] do
2012-01-13 11:44:27 -05:00
first_name "John"
last_name "Doe"
date_of_birth { 18.years.ago }
end
factory :post do
author
# instead of
2012-03-31 11:36:20 -04:00
# association :author, factory: :user
2012-01-13 11:44:27 -05:00
title "How to read a book effectively"
body "There are five steps involved."
end
factory :comment do
commenter
# instead of
2012-03-31 11:36:20 -04:00
# association :commenter, factory: :user
2012-01-13 11:44:27 -05:00
body "Great article!"
end
```
2011-07-01 11:14:27 -04:00
2011-02-07 17:48:29 -05:00
Dependent Attributes
--------------------
2012-02-08 10:17:57 -05:00
Attributes can be based on the values of other attributes using the evaluator that is yielded to lazy attribute blocks:
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
2012-03-31 11:36:20 -04:00
first_name "Joe"
last_name "Blow"
2012-01-13 11:44:27 -05:00
email { "#{first_name}.#{last_name}@example.com".downcase }
end
2011-02-07 17:48:29 -05:00
2012-03-31 11:36:20 -04:00
FactoryGirl.create(:user, last_name: "Doe").email
2012-01-13 11:44:27 -05:00
# => "joe.doe@example.com"
```
2011-02-07 17:48:29 -05:00
2011-08-20 18:05:41 -04:00
Transient Attributes
--------------------
There may be times where your code can be DRYed up by passing in transient attributes to factories.
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
ignore do
rockstar true
2012-03-31 11:36:20 -04:00
upcased false
2012-01-13 11:44:27 -05:00
end
2011-08-20 18:05:41 -04:00
2012-01-13 11:44:27 -05:00
name { "John Doe#{" - Rockstar" if rockstar}" }
email { "#{name.downcase}@example.com" }
2011-08-20 18:05:41 -04:00
2012-02-08 10:17:57 -05:00
after_create do |user, evaluator|
user.name.upcase! if evaluator.upcased
2012-01-13 11:44:27 -05:00
end
end
2011-08-20 18:05:41 -04:00
2012-03-31 11:36:20 -04:00
FactoryGirl.create(:user, upcased: true).name
2012-01-13 11:44:27 -05:00
#=> "JOHN DOE - ROCKSTAR"
```
2011-08-20 18:05:41 -04:00
2011-09-16 09:27:05 -04:00
Static and dynamic attributes can be ignored. Ignored attributes will be ignored
within attributes\_for and won't be set on the model, even if the attribute
exists or you attempt to override it.
2011-08-20 18:05:41 -04:00
2011-09-16 09:27:05 -04:00
Within Factory Girl's dynamic attributes, you can access ignored attributes as
2012-02-08 10:17:57 -05:00
you would expect. If you need to access the evaluator in a Factory Girl callback,
you'll need to declare a second block argument (for the evaluator) and access
2011-09-16 09:27:05 -04:00
ignored attributes from there.
2011-08-20 18:05:41 -04:00
2011-02-07 17:48:29 -05:00
Associations
------------
2011-08-25 06:39:54 -04:00
It's possible to set up associations within factories. If the factory name is the same as the association name, the factory name can be left out.
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
```ruby
factory :post do
# ...
author
end
```
2011-02-07 17:48:29 -05:00
You can also specify a different factory or override attributes:
2012-01-13 11:44:27 -05:00
```ruby
factory :post do
# ...
2012-03-31 11:36:20 -04:00
association :author, factory: :user, last_name: "Writely"
2012-01-13 11:44:27 -05:00
end
```
2011-02-07 17:48:29 -05:00
The behavior of the association method varies depending on the build strategy used for the parent object.
2012-01-13 11:44:27 -05:00
```ruby
# Builds and saves a User and a Post
post = FactoryGirl.create(:post)
post.new_record? # => false
post.author.new_record? # => false
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
# Builds and saves a User, and then builds but does not save a Post
post = FactoryGirl.build(:post)
post.new_record? # => true
post.author.new_record? # => false
```
2011-08-10 16:26:29 -04:00
2012-03-31 11:36:20 -04:00
To not save the associated object, specify strategy: :build in the factory:
2011-08-10 16:26:29 -04:00
2012-01-13 11:44:27 -05:00
```ruby
factory :post do
# ...
2012-03-31 11:36:20 -04:00
association :author, factory: :user, strategy: :build
2012-01-13 11:44:27 -05:00
end
2011-08-10 16:26:29 -04:00
2012-01-13 11:44:27 -05:00
# Builds a User, and then builds a Post, but does not save either
post = FactoryGirl.build(:post)
post.new_record? # => true
post.author.new_record? # => true
```
2011-02-07 17:48:29 -05:00
2012-02-17 15:20:46 -05:00
Generating data for a `has_many` relationship is a bit more involved,
depending on the amount of flexibility desired, but here's a surefire example
of generating associated data.
```ruby
FactoryGirl.define do
# post factory with a `belongs_to` association for the user
factory :post do
title "Through the Looking Glass"
user
end
# user factory without associated posts
factory :user do
name "John Doe"
# user_with_posts will create post data after the user has been created
factory :user_with_posts do
# posts_count is declared as an ignored attribute and available in
# attributes on the factory, as well as the callback via the evaluator
ignore do
posts_count 5
end
# the after_create yields two values; the user instance itself and the
# evaluator, which stores all values from the factory, including ignored
# attributes; `create_list` 's second argument is the number of records
# to create and we make sure the user is associated properly to the post
after_create do |user, evaluator|
2012-03-31 11:36:20 -04:00
FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
2012-02-17 15:20:46 -05:00
end
end
end
end
```
This allows us to do:
```ruby
FactoryGirl.create(:user).posts.length # 0
FactoryGirl.create(:user_with_posts).posts.length # 5
2012-03-31 11:36:20 -04:00
FactoryGirl.create(:user_with_posts, posts_count: 15).posts.length # 15
2012-02-17 15:20:46 -05:00
```
2011-02-07 17:48:29 -05:00
Inheritance
-----------
2011-06-29 16:49:45 -04:00
You can easily create multiple factories for the same class without repeating common attributes by nesting factories:
2012-01-13 11:44:27 -05:00
```ruby
factory :post do
2012-03-31 11:36:20 -04:00
title "A title"
2011-06-29 16:49:45 -04:00
2012-01-13 11:44:27 -05:00
factory :approved_post do
approved true
end
end
2011-06-29 16:49:45 -04:00
2012-01-13 11:44:27 -05:00
approved_post = FactoryGirl.create(:approved_post)
2012-03-31 11:36:20 -04:00
approved_post.title # => "A title"
2012-01-13 11:44:27 -05:00
approved_post.approved # => true
```
2011-06-29 16:49:45 -04:00
You can also assign the parent explicitly:
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
```ruby
factory :post do
2012-03-31 11:36:20 -04:00
title "A title"
2012-01-13 11:44:27 -05:00
end
2011-06-29 11:46:59 -04:00
2012-03-31 11:36:20 -04:00
factory :approved_post, parent: :post do
2012-01-13 11:44:27 -05:00
approved true
end
```
2011-02-07 17:48:29 -05:00
2011-09-16 09:27:05 -04:00
As mentioned above, it's good practice to define a basic factory for each class
with only the attributes required to create it. Then, create more specific
factories that inherit from this basic parent. Factory definitions are still
code, so keep them DRY.
2011-06-29 12:25:42 -04:00
2011-02-07 17:48:29 -05:00
Sequences
---------
Unique values in a specific format (for example, e-mail addresses) can be
generated using sequences. Sequences are defined by calling sequence in a
definition block, and values in a sequence are generated by calling
2011-08-17 03:27:05 -04:00
FactoryGirl.generate:
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
```ruby
# Defines a new sequence
FactoryGirl.define do
sequence :email do |n|
"person#{n}@example.com"
end
end
2011-06-29 11:46:59 -04:00
2012-01-13 11:44:27 -05:00
FactoryGirl.generate :email
# => "person1@example.com"
2011-06-29 11:46:59 -04:00
2012-01-13 11:44:27 -05:00
FactoryGirl.generate :email
# => "person2@example.com"
```
2011-02-07 17:48:29 -05:00
Sequences can be used as attributes:
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
email
end
```
2011-02-07 17:48:29 -05:00
Or in lazy attributes:
2012-01-13 11:44:27 -05:00
```ruby
factory :invite do
invitee { FactoryGirl.generate(:email) }
end
```
2011-02-07 17:48:29 -05:00
And it's also possible to define an in-line sequence that is only used in
a particular factory:
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
sequence(:email) {|n| "person#{n}@example.com" }
end
```
2011-02-07 17:48:29 -05:00
2011-06-27 17:33:58 -04:00
You can also override the initial value:
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
sequence(:email, 1000) {|n| "person#{n}@example.com" }
end
```
2011-06-27 17:33:58 -04:00
Without a block, the value will increment itself, starting at its initial value:
2012-01-13 11:44:27 -05:00
```ruby
factory :post do
sequence(:position)
end
```
2011-06-27 17:33:58 -04:00
2011-08-12 16:16:17 -04:00
Traits
------
2011-08-12 15:25:58 -04:00
2011-08-12 16:16:17 -04:00
Traits allow you to group attributes together and then apply them
2011-08-12 15:25:58 -04:00
to any factory.
2012-01-13 11:44:27 -05:00
```ruby
2012-03-31 11:36:20 -04:00
factory :user, aliases: [:author]
2011-08-12 15:25:58 -04:00
2012-01-13 11:44:27 -05:00
factory :story do
title "My awesome story"
author
2011-08-12 15:25:58 -04:00
2012-01-13 11:44:27 -05:00
trait :published do
published true
end
2011-08-12 15:25:58 -04:00
2012-01-13 11:44:27 -05:00
trait :unpublished do
published false
end
2011-08-12 15:25:58 -04:00
2012-01-13 11:44:27 -05:00
trait :week_long_publishing do
start_at { 1.week.ago }
end_at { Time.now }
end
2011-08-12 15:25:58 -04:00
2012-01-13 11:44:27 -05:00
trait :month_long_publishing do
start_at { 1.month.ago }
end_at { Time.now }
end
2011-08-12 15:25:58 -04:00
2012-03-31 11:36:20 -04:00
factory :week_long_published_story, traits: [:published, :week_long_publishing]
factory :month_long_published_story, traits: [:published, :month_long_publishing]
factory :week_long_unpublished_story, traits: [:unpublished, :week_long_publishing]
factory :month_long_unpublished_story, traits: [:unpublished, :month_long_publishing]
2012-01-13 11:44:27 -05:00
end
```
2011-08-12 15:25:58 -04:00
2011-08-12 16:16:17 -04:00
Traits can be used as attributes:
2011-08-12 15:25:58 -04:00
2012-01-13 11:44:27 -05:00
```ruby
2012-03-31 11:36:20 -04:00
factory :week_long_published_story_with_title, parent: :story do
2012-01-13 11:44:27 -05:00
published
week_long_publishing
title { "Publishing that was started at {start_at}" }
end
```
2011-08-12 15:25:58 -04:00
2011-08-12 16:16:17 -04:00
Traits that define the same attributes won't raise AttributeDefinitionErrors;
the trait that defines the attribute latest gets precedence.
2011-08-12 15:25:58 -04:00
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
name "Friendly User"
login { name }
trait :male do
name "John Doe"
gender "Male"
login { "#{name} (M)" }
end
trait :female do
name "Jane Doe"
gender "Female"
login { "#{name} (F)" }
end
trait :admin do
admin true
login { "admin-#{name}" }
end
2012-03-31 11:36:20 -04:00
factory :male_admin, traits: [:male, :admin] # login will be "admin-John Doe"
factory :female_admin, traits: [:admin, :female] # login will be "Jane Doe (F)"
2012-01-13 11:44:27 -05:00
end
```
2011-08-12 15:25:58 -04:00
2011-09-23 13:14:02 -04:00
You can also override individual attributes granted by a trait in subclasses.
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
name "Friendly User"
login { name }
2011-09-23 13:14:02 -04:00
2012-01-13 11:44:27 -05:00
trait :male do
name "John Doe"
gender "Male"
login { "#{name} (M)" }
end
2011-09-23 13:14:02 -04:00
2012-01-13 11:44:27 -05:00
factory :brandon do
male
name "Brandon"
end
end
```
2011-09-23 13:14:02 -04:00
Traits can be added to factories when the factory creates an instance
This allows for traits to be used with normal factories without having
to name every single factory that uses one (or many) traits.
So, instead of creating male_admin and female_admin factories:
FactoryGirl.define do
factory :user do
trait(:admin) { admin true }
trait(:male) { gender "Male" }
trait(:female) { gender "Female" }
factory :male_admin, :traits => [:male, :admin]
factory :female_admin, :traits => [:admin, :female]
end
end
FactoryGirl.create(:male_admin)
FactoryGirl.create(:female_admin)
You could just create a user with those traits assigned:
FactoryGirl.create(:user, :admin, :male)
FactoryGirl.create(:user, :admin, :female)
This can be combined with attribute overrides as expected.
FactoryGirl.create(:user, :admin, :male, :name => "John Doe")
FactoryGirl.create(:user, :admin, :female, :name => "Jane Doe")
2011-11-18 09:25:49 -05:00
Traits can also be passed in as a list of symbols when you construct an instance from FactoryGirl.
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
name "Friendly User"
Traits can be added to factories when the factory creates an instance
This allows for traits to be used with normal factories without having
to name every single factory that uses one (or many) traits.
So, instead of creating male_admin and female_admin factories:
FactoryGirl.define do
factory :user do
trait(:admin) { admin true }
trait(:male) { gender "Male" }
trait(:female) { gender "Female" }
factory :male_admin, :traits => [:male, :admin]
factory :female_admin, :traits => [:admin, :female]
end
end
FactoryGirl.create(:male_admin)
FactoryGirl.create(:female_admin)
You could just create a user with those traits assigned:
FactoryGirl.create(:user, :admin, :male)
FactoryGirl.create(:user, :admin, :female)
This can be combined with attribute overrides as expected.
FactoryGirl.create(:user, :admin, :male, :name => "John Doe")
FactoryGirl.create(:user, :admin, :female, :name => "Jane Doe")
2011-11-18 09:25:49 -05:00
2012-01-13 11:44:27 -05:00
trait :male do
name "John Doe"
gender "Male"
end
Traits can be added to factories when the factory creates an instance
This allows for traits to be used with normal factories without having
to name every single factory that uses one (or many) traits.
So, instead of creating male_admin and female_admin factories:
FactoryGirl.define do
factory :user do
trait(:admin) { admin true }
trait(:male) { gender "Male" }
trait(:female) { gender "Female" }
factory :male_admin, :traits => [:male, :admin]
factory :female_admin, :traits => [:admin, :female]
end
end
FactoryGirl.create(:male_admin)
FactoryGirl.create(:female_admin)
You could just create a user with those traits assigned:
FactoryGirl.create(:user, :admin, :male)
FactoryGirl.create(:user, :admin, :female)
This can be combined with attribute overrides as expected.
FactoryGirl.create(:user, :admin, :male, :name => "John Doe")
FactoryGirl.create(:user, :admin, :female, :name => "Jane Doe")
2011-11-18 09:25:49 -05:00
2012-01-13 11:44:27 -05:00
trait :admin do
admin true
end
end
Traits can be added to factories when the factory creates an instance
This allows for traits to be used with normal factories without having
to name every single factory that uses one (or many) traits.
So, instead of creating male_admin and female_admin factories:
FactoryGirl.define do
factory :user do
trait(:admin) { admin true }
trait(:male) { gender "Male" }
trait(:female) { gender "Female" }
factory :male_admin, :traits => [:male, :admin]
factory :female_admin, :traits => [:admin, :female]
end
end
FactoryGirl.create(:male_admin)
FactoryGirl.create(:female_admin)
You could just create a user with those traits assigned:
FactoryGirl.create(:user, :admin, :male)
FactoryGirl.create(:user, :admin, :female)
This can be combined with attribute overrides as expected.
FactoryGirl.create(:user, :admin, :male, :name => "John Doe")
FactoryGirl.create(:user, :admin, :female, :name => "Jane Doe")
2011-11-18 09:25:49 -05:00
2012-01-13 11:44:27 -05:00
# creates an admin user with gender "Male" and name "Jon Snow"
2012-03-31 11:36:20 -04:00
FactoryGirl.create(:user, :admin, :male, name: "Jon Snow")
2012-01-13 11:44:27 -05:00
```
Traits can be added to factories when the factory creates an instance
This allows for traits to be used with normal factories without having
to name every single factory that uses one (or many) traits.
So, instead of creating male_admin and female_admin factories:
FactoryGirl.define do
factory :user do
trait(:admin) { admin true }
trait(:male) { gender "Male" }
trait(:female) { gender "Female" }
factory :male_admin, :traits => [:male, :admin]
factory :female_admin, :traits => [:admin, :female]
end
end
FactoryGirl.create(:male_admin)
FactoryGirl.create(:female_admin)
You could just create a user with those traits assigned:
FactoryGirl.create(:user, :admin, :male)
FactoryGirl.create(:user, :admin, :female)
This can be combined with attribute overrides as expected.
FactoryGirl.create(:user, :admin, :male, :name => "John Doe")
FactoryGirl.create(:user, :admin, :female, :name => "Jane Doe")
2011-11-18 09:25:49 -05:00
This ability works with `build` , `build_stubbed` , `attributes_for` , and `create` .
2012-02-07 10:19:22 -05:00
`create_list` and `build_list` methods are supported as well. Just remember to pass
the number of instances to create/build as second parameter, as documented in the
"Building or Creating Multiple Records" section of this file.
```ruby
factory :user do
name "Friendly User"
trait :admin do
admin true
end
end
# creates 3 admin users with gender "Male" and name "Jon Snow"
2012-03-31 11:36:20 -04:00
FactoryGirl.create_list(:user, 3, :admin, :male, name: "Jon Snow")
2012-02-07 10:19:22 -05:00
```
2011-02-07 17:48:29 -05:00
Callbacks
---------
2011-09-16 09:27:05 -04:00
factory\_girl makes available three callbacks for injecting some code:
2011-02-07 17:48:29 -05:00
* after_build - called after a factory is built (via FactoryGirl.build)
* after_create - called after a factory is saved (via FactoryGirl.create)
2012-01-28 19:06:41 -05:00
* after_stub - called after a factory is stubbed (via FactoryGirl.build_stubbed)
2011-02-07 17:48:29 -05:00
Examples:
2012-01-13 11:44:27 -05:00
```ruby
# Define a factory that calls the generate_hashed_password method after it is built
factory :user do
after_build { |user| generate_hashed_password(user) }
end
```
2011-02-07 17:48:29 -05:00
2011-07-01 11:07:02 -04:00
Note that you'll have an instance of the user in the block. This can be useful.
2011-02-07 17:48:29 -05:00
You can also define multiple types of callbacks on the same factory:
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
after_build { |user| do_something_to(user) }
after_create { |user| do_something_else_to(user) }
end
```
2011-02-07 17:48:29 -05:00
Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:
2012-01-13 11:44:27 -05:00
```ruby
factory :user do
after_create { this_runs_first }
after_create { then_this }
end
```
2011-02-07 17:48:29 -05:00
2011-09-16 09:27:05 -04:00
Calling FactoryGirl.create will invoke both after\_build and after\_create callbacks.
2011-02-07 17:48:29 -05:00
Also, like standard attributes, child factories will inherit (and can also define) callbacks from their parent factory.
2011-09-02 12:05:00 -04:00
Modifying factories
-------------------
If you're given a set of factories (say, from a gem developer) but want to change them to fit into your application better, you can
modify that factory instead of creating a child factory and adding attributes there.
If a gem were to give you a User factory:
2012-01-13 11:44:27 -05:00
```ruby
FactoryGirl.define do
factory :user do
full_name "John Doe"
sequence(:username) {|n| "user#{n}" }
password "password"
end
end
```
2011-09-02 12:05:00 -04:00
Instead of creating a child factory that added additional attributes:
2012-01-13 11:44:27 -05:00
```ruby
FactoryGirl.define do
2012-03-31 11:36:20 -04:00
factory :application_user, parent: :user do
2012-01-13 11:44:27 -05:00
full_name { Faker::Name.name }
date_of_birth { 21.years.ago }
gender "Female"
health 90
end
end
```
2011-09-02 12:05:00 -04:00
You could modify that factory instead.
2012-01-13 11:44:27 -05:00
```ruby
FactoryGirl.modify do
factory :user do
full_name { Faker::Name.name }
date_of_birth { 21.years.ago }
gender "Female"
health 90
end
end
```
2011-09-02 12:05:00 -04:00
When modifying a factory, you can change any of the attributes you want (aside from callbacks).
`FactoryGirl.modify` must be called outside of a `FactoryGirl.define` block as it operates on factories differently.
2011-09-23 13:14:02 -04:00
A caveat: you can only modify factories (not sequences or traits) and callbacks *still compound as they normally would* . So, if
2011-09-02 12:05:00 -04:00
the factory you're modifying defines an `after_create` callback, you defining an `after_create` won't override it, it'll just get run after the first callback.
2011-01-04 16:12:04 -05:00
Building or Creating Multiple Records
-------------------------------------
Sometimes, you'll want to create or build multiple instances of a factory at once.
2012-01-13 11:44:27 -05:00
```ruby
built_users = FactoryGirl.build_list(:user, 25)
created_users = FactoryGirl.create_list(:user, 25)
```
2011-01-04 16:12:04 -05:00
These methods will build or create a specific amount of factories and return them as an array.
To set the attributes for each of the factories, you can pass in a hash as you normally would.
2012-01-13 11:44:27 -05:00
```ruby
2012-03-31 11:36:20 -04:00
twenty_year_olds = FactoryGirl.build_list(:user, 25, date_of_birth: 20.years.ago)
2012-01-13 11:44:27 -05:00
```
2011-01-04 16:12:04 -05:00
2012-01-20 13:04:48 -05:00
Custom Construction
-------------------
2012-01-23 09:51:14 -05:00
If you want to use factory_girl to construct an object where some attributes
are passed to `initialize` or if you want to do something other than simply
calling `new` on your build class, you can override the default behavior by
defining `to_initialize` on your factory. Example:
2012-01-20 13:04:48 -05:00
```ruby
# user.rb
class User
attr_accessor :name, :email
def initialize(name)
@name = name
end
end
# factories.rb
sequence(:name) {|n| "person#{n}@example.com" }
factory :user do
ignore do
name { Faker::Name.name }
end
email
initialize_with { User.new(name) }
end
FactoryGirl.build(:user).name # Bob Hope
```
Notice that I ignored the `name` attribute. If you don't want attributes
reassigned after your object has been instantiated, you'll want to `ignore` them.
2012-01-23 09:51:14 -05:00
Although factory_girl is written to work with ActiveRecord out of the box, it
can also work with any Ruby class. For maximum compatibiltiy with ActiveRecord,
the default initializer builds all instances by calling new on your build class
without any arguments. It then calls attribute writer methods to assign all the
attribute values. While that works fine for ActiveRecord, it actually doesn't
work for almost any other Ruby class.
You can override the initializer in order to:
* Build non-ActiveRecord objects that require arguments to `initialize`
* Use a method other than `new` to instantiate the instance
* Do crazy things like decorate the instance after it's built
2011-07-28 09:57:07 -04:00
Cucumber Integration
--------------------
2011-09-16 09:27:05 -04:00
factory\_girl ships with step definitions that make calling factories from Cucumber easier. To use them, add the following to features/support/env.rb:
2011-07-28 09:57:07 -04:00
2012-01-13 11:44:27 -05:00
```ruby
2012-03-31 11:36:20 -04:00
require "factory_girl/step_definitions"
2012-01-13 11:44:27 -05:00
```
2011-07-28 09:57:07 -04:00
2011-02-07 17:48:29 -05:00
Alternate Syntaxes
------------------
2011-09-16 09:27:05 -04:00
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. For example, the Machinist-style syntax is popular:
2011-02-07 17:48:29 -05:00
2012-01-13 11:44:27 -05:00
```ruby
2012-03-31 11:36:20 -04:00
require "factory_girl/syntax/blueprint"
require "factory_girl/syntax/make"
require "factory_girl/syntax/sham"
2011-06-29 11:46:59 -04:00
2012-01-13 11:44:27 -05:00
Sham.email {|n| "#{n}@example.com" }
2011-06-29 11:46:59 -04:00
2012-01-13 11:44:27 -05:00
User.blueprint do
2012-03-31 11:36:20 -04:00
name { "Billy Bob" }
2012-01-13 11:44:27 -05:00
email { Sham.email }
end
2011-06-29 11:46:59 -04:00
2012-03-31 11:36:20 -04:00
User.make(name: "Johnny")
2012-01-20 13:04:48 -05:00
```