【问题标题】:Unable to detrmine if a test failed in an after hook无法确定测试是否在后挂钩中失败
【发布时间】:2019-08-05 13:33:12
【问题描述】:

当测试失败时,我会尝试做一些额外的事情。

这是失败的测试:

it 'fails' do
  expect(1213).to eq('123456')
end

以下代码被添加到 spec_helper 中:

RSpec.configure do |config|
  config.after(:each) do |example|
    if example.exception
      puts 'Do something'
    end
  end

结果如下:

预期:“123456” 得到:“1213” (使用 == 比较)

1 个示例,1 个失败,0 个通过

2秒完成

示例中的异常保持为零,我不明白为什么。测试失败后还有其他方法可以实现额外的代码吗?

【问题讨论】:

标签: ruby rspec automated-tests


【解决方案1】:

这是答案(以及解决方案的想法)https://github.com/rspec/rspec-core/issues/2011#issuecomment-114669886

无论如何,直到 after 钩子运行之后才设置状态。这是故意的,因为 after 钩子本身就是示例的一部分,如果在 after 钩子中发生异常,我们会将示例的状态设置为 :failed。 after 钩子非常适合清理/拆卸逻辑,但不适用于观察示例的状态。相反,我建议您为此使用格式化程序...

【讨论】:

    【解决方案2】:

    代码应该可以完美运行,您应该在 STDOUT 上打印“F”之前看到“做某事”,如下所示:

    Randomized with seed 3598
    Do something
    F
    
    Failures:
    
      1) Blah#blah fails
         Failure/Error: expect(1213).to eq('123456')
    
           expected: "123456"
                got: 1213
    
           (compared using ==)
         # ./spec/index_spec.rb:9:in `block (2 levels) in <top (required)>'
    
    Finished in 0.01617 seconds (files took 0.09568 seconds to load)
    1 example, 1 failure, 0 passed
    

    还要注意您使用的是哪个 rspec 版本,您的测试报告看起来与我的不同。我的是 v3.8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 2013-08-26
      • 1970-01-01
      • 2013-03-17
      • 2019-08-10
      • 1970-01-01
      相关资源
      最近更新 更多