Reputation: 4811
factory :account do
end
factory :user do
account
end
factory :user2 do
account
end
factory :location do
// how to add to the users has_many collection?
end
My Location model has a has_many :users
association, how can I add user and user2 to that collection in my factories?
Also when building an object graph, should factory_girl be used just to create an Account, User but not actually build the relation between the two? Should I be doing that myself in my tests or some kind of helper? Because say I want to create account1 and account2, and each account has a location. I have to build this object graph myself within my tests then right?
Upvotes: 1
Views: 556
Reputation: 3722
you can create 2 users at once:
factory :location do
after(:create) do |location|
create_list(:user, 2, location: location)
end
end
Upvotes: 1
Reputation: 738
You have to use the after
callback:
factory :location do
after(:create) do |location, evaluator|
create(:user, location: location)
create(:user2, location: location)
end
end
See the documentation
Upvotes: 3