Hello.
I'm using Factory Girl/Rspec2/Rails 3.
In factories.rb, I have:
Factory.define :user do |user|
user.name 'Some guy'
user.email '[email protected]'
user.password 'password'
end
Factory.define :org_admin, :parent => :user do |user|
user.email '[email protected]'
end
Factory.define :user_with_membership_request, :parent => :user do |user|
user.email '[email protected]'
end
Factory.define :organization do |org|
org.name 'MEC'
org.description 'Mountain Equipment Co-op'
end
Factory.define :membership do |membership|
membership.user { Factory(:user) }
membership.organization { Factory(:organization) }
end
Factory.define :admin_membership, :parent => :membership do |membership|
membership.user { Factory(:org_admin) }
membership.is_admin true
membership.status 'active'
end
Factory.define :membership_request, :parent => :membership do |membership|
membership.user { Factory(:user_with_membership_request) }
membership.status 'requested'
end
and then in my rspec test I have:
it 'should accept the membership request' do
@org_admin = Factory(:org_admin)
test_sign_in(@org_admin)
@organization = Factory(:organization)
@membership_request = Factory(:membership_request)
put :update, :organization_id => @organization.id, :id => @membership_request.id, :acceptance => 'approve'
...
end
When I run the test, I get:
Failure/Error: @membership_request = Factory(:membership_request)
Validation failed: Name has already been taken
I understand the reason for the failure is because FactoryGirl is creating another organization (with the same name).
But what I'd like to do is create several memberships all associated with the same organization. How do I do that?
Thanks.
Sean