thoughtbot/factory_girl — pull request #240 by joshuaclayton (2011-11-18 19:02:18+00:00)
Title: 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")