我試圖創建一些相關的工廠,但事件不工作: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)>'
非常感謝您對您可以在此提供任何意見!
一個要檢查的地方可能是模式文件。如果爲事件表指定了任何約束(這在工廠中沒有考慮),那麼它可能會拋出ConstraintException。 – BumbleBoks 2012-05-03 06:33:03