Reputation: 25542
Here are my factories:
Users.rb
FactoryGirl.define do
sequence(:email) do |n|
"user#{n}@example.com"
end
factory :user do
email
first_name Faker::Name.first_name
last_name Faker::Name.last_name
password "password"
password_confirmation "password"
agreed_to_age_requirements true
username "testing123"
state "AL"
city_id 201
school_id 20935
handedness "Left"
customer_id { "#{rand(1000)}" }
after(:create) do |user, elevator|
user.subscriptions << FactoryGirl.create(:subscription, account_type_name: "#{elevator.account_type_name}")
user.sports << FactoryGirl.create(:sport)
user.roles << FactoryGirl.create(:role)
end
end
factory :athlete, class: "Athlete", parent: :user do
type "Athlete"
recruit_year "2016"
end
end
Subscriptions.rb
FactoryGirl.define do
factory :subscription do
trial_expiry 30.days.from_now
active true
after :create do |subscription, elevator|
account_type {create(:account_type, name: "#{elevator.account_type_name}", price: 0)}
end
end
end
AccountTypes.rb
FactoryGirl.define do
factory :account_type do
name "Legend"
price 15
trial_period_days 0
videos 20
contributors 15
end
end
Here is what my test looks like:
before :each do
@user = create(:user)
@sport_user = create(:user, sports: [])
@school_admin_role = create(:role, name: "School Admin")
@contributor_role = create(:role, name: "Contributor")
end
The problem is that when I create a second user, the Account Type that is associated with the first user's subscription has already been created and so the same account type already exists in my testing db. Any way to write this so that this doesn't happen?
Upvotes: 0
Views: 360
Reputation: 13344
Here's what I'm trying to get at in my comment above. If this doesn't work, let me know and I'll delete the answer.
Users.rb
FactoryGirl.define do
sequence(:email) do |n|
"user#{n}@example.com"
end
factory :user do
email
first_name Faker::Name.first_name
last_name Faker::Name.last_name
password "password"
password_confirmation "password"
agreed_to_age_requirements true
username "testing123"
state "AL"
city_id 201
school_id 20935
handedness "Left"
customer_id { "#{rand(1000)}" }
subscriptions {[create(:subscription, account_type_name: "#{account_type_name}")]}
roles {[create(:role)]}
after(:create) do |user|
user.sports << FactoryGirl.create(:sport)
end
end
factory :athlete, class: "Athlete", parent: :user do
type "Athlete"
recruit_year "2016"
end
end
Subscriptions.rb
FactoryGirl.define do
factory :subscription do
trial_expiry 30.days.from_now
active true
after(:create) do |subscription, evaluator|
account_type {create(:account_type, name: "#{evaluator.account_type_name}", price: 0)}
end
end
end
AccountTypes.rb
FactoryGirl.define do
factory :account_type do
name "Legend"
price 15
trial_period_days 0
videos 20
contributors 15
end
end
Here is what my test looks like:
before :each do
@school_admin_role = create(:role, name: "School Admin")
@contributor_role = create(:role, name: "Contributor")
@user = create(:user, account_type_name: "Free")
@sport_user = create(:user, sports: [], account_type_name: "Other Free Name")
end
Upvotes: 1