我正在使用rails 5和rspec 3.6.0。我有3个模型User,Organization和UserOrganization。用户可以属于多个组织。我们维护与'current_organization'的关联,'current_organization'是用户当前登录的组织。 我已将用户的工厂定义为
FactoryGirl.define do
factory :user do
email { FFaker::Internet.email }
factory :admin_with_organization do
after(:build) do |user|
org = create :organization
org_user = build(:organization_user, organization: org, user: user)
org_user.add_role :administrator
user.current_organization = org
user.current_role = org_user.roles.first
end
end
factory :doctor_with_organization do
after(:build) do |user|
org = create :organization
org_user = build(:organization_user, organization: org, user: user)
org_user.add_role :doctor
user.current_organization = org
user.current_role = org_user.roles.first
end
end
...
end
是否可以将组织对象传递给上述工厂,以便可以创建属于同一组织的所有多个用户?