expect(target).to receive(:message).with(arg_matcher)
如果使用不匹配传递给的arg匹配器的参数调用目标,则rspec-mocks '仅在测试结束时显示错误with
。有没有办法迫使它急于失败-即,一旦使用不匹配的参数调用目标?RR以这种方式工作。
我面临的问题是,当我如上所述使用arg_matcher设置此模拟程序时,由于目标调用了不同的参数,因此测试开始失败,但是在测试结束之前另一个断言失败了,所以我只看到错误从这个断言中获取,而不是从缺少的模拟中获取(这将使我看到预期的参数与实际调用的参数之间的差异)。
使用rspec-mocks 3.3.2。
我不知道一种receive
急于失败的方法。但是,have_received
急于失败,因此,如果这是多个期望中的第一个,它将是未通过测试且RSpec报告的那个。
class Foo
def self.bar(baz)
end
end
describe "RSpec" do
it "reports a non-mock expectation failure before a mock expectation failure" do
expect(Foo).to receive(:bar).with(1)
expect(true).to be_falsy # RSpec reports this failure
Foo.bar 2
end
it "reports a spy expectation failure when you'd expect it to be reported" do
allow(Foo).to receive(:bar) # Spy on Foo
Foo.bar 2
expect(Foo).to have_received(:bar).with(1) # RSpec reports this failure
expect(true).to be_falsy
end
end
有关详细信息,请参见RSpec间谍的文档。
这种解决方案可能是最好的,如果
allow
,设置了间谍不到你介意aggregate_failures
块常规模拟和Adarsh的解决方案更好
aggregate_failures
障碍物的关注程度不及allow
对间谍物的额外输入,那么您可以设置间谍本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句