0
下面的rspec测试失败了,但失败测试旁边提供的消息似乎是预期的结果。为什么rspec'期望raise_error'失败,当返回的消息是'ActiveRecord :: RecordInvalid'
describe '#validate_maximum_pending_actions_not_been_reached' do
let(:action) { build :action, status: 'pending' }
before :each do
10.times do
create :action, status: 'pending'
end
end
it 'does not save the 11th action' do
expect(action.save).to raise_error #(ActiveRecord::RecordInvalid,'Validation failed: maximum number of pending actions already reached')
end
end
Failure/Error: let(:action) { build :action, status: 'pending' }
ActiveRecord::RecordInvalid:
Validation failed: maximum number of pending actions already reached
我曾尝试是通过省略该错误信息,并简单地希望较不具体的是,测试将raise_error
。测试仍然失败,但给出了上面的说明,表明确实出现了错误。
我在做什么错?
被测试的方法如下:
validate :validate_maximum_pending_actions_not_been_reached
def validate_maximum_pending_actions_not_been_reached
errors[:base] << "maximum number of pending actions already reached" unless Action.where(status: 'pending').size <= 10
end
谢谢@gasburn,知道我一定忽略了一些明显的东西! –