2012-04-05 12 views
0

私はいくつかの関連した工場を作成しようとしていますが、イベントが動作しない:FactoryGirl RSpecのとSQLite3の:: ConstraintException

factories.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)>' 

は、あなたがこの上で提供できるすべてのアドバイスをありがとうございました!

+0

チェックする場所はスキーマファイルです。イベント・テーブルに指定された制約がある場合(工場では扱われていない)、ConstraintExceptionがスローされることがあります。 – BumbleBoks

答えて

0

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

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

+0

私はこの場合、備品など何も持っていません。私が持っているのは、テストするこれらの数少ない工場だけです。どのように紛争が起こるのでしょうか? – Frog

+0

さて、あなたはコマンドラインのテストなどでデータをdbに追加できました。 sqliteブラウザを使用してdbを表示することができます。 – rOrlig

関連する問題