我有一個驗證名稱和條款驗收RSpec的捕捉驗證錯誤
class Product < ActiveRecord::Base
validates :name, presence: true,
length: { maximum: 50 }, on: :update
validates_acceptance_of :agreed_to_terms, on: :update
我寫了一個簡單的測試
it 'redirects back if there is missing info' do
post "/product/1", {:name => "john"}
expect(ActiveRecord).to raise_error(StatementInvalid)
end
我似乎無法捕獲錯誤的模型。
ActiveRecord::StatementInvalid:
SQLite3::ConstraintException: NOT NULL constraint failed:
你確定這個錯誤實際上是提出?通常,驗證只是簡單地導致模型返回'false'來表示'valid?'。 – zetetic 2015-02-12 02:19:36
驗證不會導致該錯誤被提出(當使用'save'時ActiveRecord :: RecordInvalid被提出 – 2015-02-12 07:01:04