测试开始失败,因为使用不同的参数调用目标,但是在测试结束之前另一个断言失败,所以我只看到这个断言的错误,而不是缺少的模拟
如果我没看错,你expect
在一个规范中有多个断言,而第一个失败了,所以你没有看到第二个/第三个断言失败?
如果是这样,我会考虑使用v3.3 中引入的RSpecaggregate_failures
,它收集失败但运行后续断言:
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
end