class Foo
include ActiveModel::Validations
def bar
errors.add(:base, 'WHAT THE HELL!?')
end
end
describe 'nonsense' do
specify do
foo = Foo.new
foo.bar
expect(foo).to have(1).error_on(:base)
end
end
Failure/Error: specify { expect(foo).to have(1).error_on(:base) }
expected 1 error on :base, got 0
describe 'nonsense' do
specify do
foo = Foo.new
foo.bar
pus foo.errors.inspect
expect(foo).to have(1).error_on(:base)
end
end
===>
#<ActiveModel::Errors:0x00000109ba51a8 @base=#<Foo:0x00000109ba51f8 @errors=#<ActiveModel::Errors:0x00000109ba51a8 ...>>, @messages={:base=>["WHAT THE HELL!?"]}>
答案 0 :(得分:0)
看来如果你没有打电话给&#34;有效吗?&#34;使用&#34;之前的方法有(n).errors_on&#34;在规范中的匹配器,它总是会返回false,即使你的错误对象中有东西!