如果使用传递给rspec-模拟的arg匹配器的参数调用目标,则rspec-模拟的expect(target).to receive(:message).with(arg_matcher)只会在测试结束时显示错误。是否有一种方法可以强迫它急切地失败--即,当目标被非匹配的参数调用时?RR以这种方式工作。
我面临的问题是,当我使用上面的arg_matcher设置这个模拟时,测试开始失败,因为目标是用不同的params调用的,但是在测试结束之前,另一个断言会失败,所以我只看到了这个断言中的错误,而不是缺失的模拟(这将向我展示预期的params和实际调用的params之间的区别)。
使用rspec-mocks 3.3.2。
发布于 2016-06-02 02:50:06
我不知道有什么办法能让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的额外输入发布于 2016-06-01 20:53:54
测试开始失败,因为目标是用不同的参数调用的,但是在测试结束之前,另一个断言失败了,所以我只看到了这个断言中的错误,而不是缺少的模拟。
如果我正确地阅读了这篇文章,那么您在一个规范中有多个expect断言,而第一个则失败了,那么您不会看到第二个/第三个断言失败吗?
如果是这样的话,我会考虑使用在3.3节中介绍,它收集故障,但运行后续断言:
aggregate_failures("verifying response") do
expect(response.status).to eq(200)
expect(response.headers).to include("Content-Type" => "text/plain")
expect(response.body).to include("Success")
end
# Which gives you nice errors for all assertions
1) Client returns a successful response
Got 3 failures:
1.1) Got 3 failures from failure aggregation block "testing response".
# ./spec/use_block_form_spec.rb:18
# ./spec/use_block_form_spec.rb:10
1.1.1) Failure/Error: expect(response.status).to eq(200)
expected: 200
got: 404
(compared using ==)
# ./spec/use_block_form_spec.rb:19
1.1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
Diff:
@@ -1,2 +1,2 @@
-[{"Content-Type"=>"application/json"}]
+"Content-Type" => "text/plain",
# ./spec/use_block_form_spec.rb:20
1.1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')
expected: "{\"message\":\"Success\"}"
got: "Not Found"
(compared using ==)
# ./spec/use_block_form_spec.rb:21
1.2) Failure/Error: expect(false).to be(true), "after hook failure"
after hook failure
# ./spec/use_block_form_spec.rb:6
# ./spec/use_block_form_spec.rb:10
1.3) Failure/Error: expect(false).to be(true), "around hook failure"
around hook failure
# ./spec/use_block_form_spec.rb:12您还可以将描述块标记为aggregate_failures
RSpec.describe ClassName, :aggregate_failures do
# stuff
endhttps://stackoverflow.com/questions/36176200
复制相似问题