0

関連するいくつかのファクトリを作成しようとしていますが、イベントが機能していません:

factory.rb

  factory :user, class: User do
    first_name 'John'
    last_name 'Doe'
    email { "#{first_name}.#{last_name}@example.com".downcase } 
    username 'johndoe'
    password 'johndoe'
    password_confirmation 'johndoe'

    association :account_id, factory: :account 
  end

  factory :account, class: Account do
    #id is the only field
  end

  factory :event, class: Event do
    name 'Go to the Dentist'
    start_date "#{Time.now.next_month}"  
    end_date "#{Time.now+1.hour.next_month}" 
    copyright "#{Time.now.year}" 

    association :account_id, factory: :account  
  end 

controller_spec.rb

before(:each) do
  @request.env["devise.mapping"] = Devise.mappings[:user]
  @user = FactoryGirl.create(:user_profile, :username => 'johndoe' )
  sign_in @user
  @acct = FactoryGirl.create(:account, :id => @user.account_id)
  @event = FactoryGirl.create(:event, :account_id => @acct.id)
end

しかし、このイベントラインはすべてがうまくいかないところです. @user.account_id を使用して :account_id をイベントに設定しても、次のエラーで失敗します。

Failure/Error: @event = FactoryGirl.create(:event, :account_id => @acct.id)
     ActiveRecord::StatementInvalid:
       SQLite3::ConstraintException: constraint failed: INSERT INTO "event" ("account_id", "copyright", "created_at", "deleted", "end_date", "info", "name", "start_date", "type", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
     # ./spec/controllers/controller_spec.rb:13:in `block (3 levels) in <top (required)>'

これについてあなたが提供できるアドバイスをありがとう!

4

1 に答える 1

1

制約の例外が発生する理由は、追加しようとしている情報と競合するデータがテーブルに既にあるためだと思います。

すべてのデータを削除してから、rspec テスト ケースを試してください。

于 2012-04-16T20:22:47.660 に答える