In my models I have the following setup:
class User < ActiveRecord::Base
has_many :assignments
has_many :roles, :through => :assignments
end
class Role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
end
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
attr_accessible :role_id, :user_id
end
In my factory.rb file I have:
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "user#{n}" }
email { "#{username}@example.com" }
password 'secret'
password_confirmation 'secret'
factory :admin do
...
end
end
factory :role do
name 'Normal'
value 'normal'
end
factory :assignment do
...
end
end
I’m struggling to figure out how I would add a role with, :name => “admin”, :value => “admin”, to the “admin” factory inside the “user” block so I can call
create(:admin)
in my tests and have a user with the admin role.
Thank you for looking.
For such a factory you need to use callbacks of factory girl.
Try this: