在每种方法后检测Rspec测试失败
我正在尝试运行RSpec测试,我想在after
方法中检测测试是否失败。 我现在有类似的东西:
after(:each) do cc = ConnectController.new() cc.update(, , result?) end
如你所见, result?
函数是我需要替换的,检测测试是否失败,以及获取有关失败的测试的信息。
除了Daniel的回答之外,在Rspec3中删除了示例方法(有关详细信息,请参阅此处 )。
你必须做这样的事情:
after(:each) do |example| if example.exception # ... end end
编辑:这个答案仅适用于RSpec 2.对于RSpec 3,请参阅geekazoid的答案。
每个块之后运行在暴露example
的类的上下文中,您可以通过检查example
上的exception
方法来检测故障:
after(:each) do if example.exception != nil # Failure only code goes here end end
我一直在寻找如何检查after(:context)
/ after(:all)
块中组中所有示例是否成功。 这就是我想出的:
after(:all) do |example_group| if example_group.class.examples.select { |ex| ex.exception }.empty? # runs only if there are no failures do('something') end end