2012-04-05 52 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