这是我的工厂:
用户.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
订阅.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
这是我的测试的样子:
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
问题是当我创建第二个用户时,与第一个用户的订阅关联的帐户类型已经创建,因此我的测试数据库中已经存在相同的帐户类型。有什么办法写这个,这样就不会发生?