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
FactoryGirl.define do
factory :user do
first_name 'John'
last_name 'Doe'
admin false
end
# This will use the User class (Admin would have been guessed)
factory :admin, :class => User do
first_name 'Admin'
last_name 'User'
admin true
end
# The same, but using a string instead of class constant
factory :admin, :class => 'user' do
first_name 'Admin'
last_name 'User'
admin true
end
end
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
---------------
factory_girl supports several different build strategies: build, create, attributes_for and stub:
# Returns a User instance that's not saved
user = FactoryGirl.build(:user)
# Returns a saved User instance
user = FactoryGirl.create(:user)
# Returns a hash of attributes that can be used to build a User instance:
attrs = FactoryGirl.attributes_for(:user)
# Returns an object with all defined attributes stubbed out:
stub = FactoryGirl.stub(:user)
No matter which strategy 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 = FactoryGirl.build(:user, :first_name => 'Joe')
user.first_name
# => "Joe"
If repeating "FactoryGirl" is too verbose for you, you can mix the syntax methods in:
# rspec
RSpec.configure do |config|
config.include Factory::Syntax::Methods
end
# Test::Unit
class Test::Unit::TestCase
include Factory::Syntax::Methods
end
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 :user do
# ...
activation_code { User.generate_activation_code }
end
Dependent Attributes
--------------------
Attributes can be based on the values of other attributes using the proxy that is yielded to lazy attribute blocks:
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: