測試啓動失敗,因爲目標是調用不同的參數,可以,但接着又斷言測試結束之前失敗,所以我只看到錯誤的這種說法,而不是從失蹤模擬
如果我正確閱讀這個內容,你在一個規範中有多個expect
斷言,第一個失敗,所以你沒有看到第二個/第三個斷言失敗?
如果是的話,我會考慮使用RSpec's aggregate_failures
introduced in v3.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
end
這是很棒的信息,謝謝!但我的問題不同 - @Dave Schweisguth說得對。 – astgtciv
Adarsh沒有完全重申你的問題,但是'aggregate_failures'的確解決了它:如果你將整個測試包裝在'aggregate_failures'中,你會看到非模擬失敗和模擬失敗。 (正如我上面所說的,你是否希望看到非模擬失敗取決於你正在測試的內容。) –