免責聲明:安東尼指出,這是不是最好的做法,但我會假設你有一個很好的理由要求。 :)
您需要構建某種形式的錯誤收集器。捕獲每個RSpec異常並將該錯誤添加到收集器中。在「掛鉤」後,檢查收集器是否包含某些內容,如果存在,則檢查是否失敗。您還需要充實收集的錯誤消息,以包含有關哪個步驟失敗以及失敗位置的更多信息。這僅僅是爲了讓你知道該怎麼做而已。
關鍵是救援和記錄您的錯誤,然後再處理它們。
test.feature
Scenario: Test out someting
Given this step passes
And this step has a collected error
Then this step passes
test_stepdef.rb
Given(/^this step passes$/) do
# keep on keeping on
end
Given(/^this step has a collected error$/) do
begin
1.should eq(0)
rescue RSpec::Expectations::ExpectationNotMetError => e
@collected_errors.push e.message
end
end
支持/ hooks.rb
Before do |scenario|
@collected_errors = []
end
After do |scenario|
fail "#{@collected_errors}" unless @collected_errors.empty?
end
輸出
Scenario: Test out someting # features/test.feature:6
Given this step passes # features/stepdefs/test_stepdef.rb:2
And this step has a collected error # features/stepdefs/test_stepdef.rb:6
Then this step passes # features/stepdefs/test_stepdef.rb:2
["expected: 0
got: 1
(compared using ==)"] (RuntimeError)
features/support/hooks.rb:21:in `After'
Failing Scenarios:
cucumber features/test.feature:6 # Scenario: Test out someting
這將違背TDD - 它總是會炸掉它命中該測試的第一種例外情形。 – Anthony 2014-11-05 16:21:05