2012-04-05 49 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 2012-05-03 06:33:03

回答

0

我想你得到約束異常的原因是你已經有一些數據在你的表中衝突你試圖添加的信息。

您可能想要刪除所有數據,然後嘗試rspec測試用例。

+0

在這種情況下,我沒有固定裝置或任何東西。我只有這幾家工廠要進行測試。怎麼會有衝突? – Frog 2012-04-19 15:50:15

+0

那麼你可以通過命令行測試或其他方式將數據添加到數據庫中。你可以使用sqlite瀏覽器來查看你的數據庫。 – rOrlig 2012-04-20 03:59:35