0

ここに私の工場があります:

ユーザー.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

問題は、2 番目のユーザーを作成するときに、最初のユーザーのサブスクリプションに関連付けられているアカウント タイプが既に作成されているため、同じアカウント タイプがテスト データベースに既に存在することです。これが起こらないようにこれを書く方法はありますか?

4

1 に答える 1