2012-05-11 80 views
0

我有一個Rails項目,並使用RSpec作爲測試框架。我需要的是當某個匹配器失敗時訂閱事件,例如我得到這個:Rspec:匹配器失敗時

true.should be_false 

我想對每一次失敗做一些操作。這是RSpec提供的功能嗎?

回答

2

你可以猴子補丁這種行爲在RSpec之中::核心:: Example類:

require 'spec_helper' 
class RSpec::Core::Example 
    def failed? 
    [email protected]? 
    end 
end 

describe "my_tested_things" do 
... 
end 

然後你就可以確保它都將失敗賽後運行所需的代碼:

after(:each) do  
    run_my_code if example.failed?  
end