简体   繁体   English

如果我正在测试rspec扩展,如何抑制在测试中失败的测试结果?

[英]If I'm testing an rspec extension, how do I suppress the results of tests which fail as part of the test?

I'm trying to write specs for an extension to rspec. 我正在尝试为rspec的扩展编写规范。

This is the gist of what I'm trying to test: 这是我要测试的要点:

require 'rspec-let-and-after-extension'

RSpec.describe "let(...).and_after" do
  it 'is called if the `let` is invoked even if the example fails' do
    call_order = []

    RSpec.describe do
      let(:foo) { }.and_after { call_order << :and_after }
      it { foo; call_order << :example; raise 'failed!' }
    end.run

    expect(call_order).to eq [:example, :and_after]
  end
end

One of the important behaviours is that if running the example fails, the cleanup code still runs. 重要的行为之一是,如果运行示例失败,则清理代码仍将运行。 So I test this by recording the order of the calls and raising an exception from the example. 因此,我通过记录调用顺序并从示例中引发异常来对此进行测试。

Problem is, when I run it, it sees this block as a second example, which then fails with errors: 问题是,当我运行它时,它将第二个示例视为该代码块,然后由于错误而失败:

.F

Failures:

  1)  
     Got 0 failures and 2 other errors:

     1.1) Failure/Error: it { foo; call_order << :example; raise 'failed!' }
          RuntimeError:
            failed!
          # ./spec/spec.rb:43:in `block (4 levels) in <top (required)>'
          # ./spec/spec.rb:44:in `block (2 levels) in <top (required)>'

     1.2) Failure/Error: it { foo; call_order << :example; raise 'failed!' }
          RuntimeError:
            failed!
          # ./spec/spec.rb:43:in `block (4 levels) in <top (required)>'

Finished in 0.00167 seconds (files took 0.08011 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/spec.rb:43 #  

As you can see, the output did have one dot, so the actual example passed . 如您所见,输出确实有一个点,因此实际示例通过了 But then there is an F, because it has seen the internal example, run that, and unsurprisingly that one failed. 但是然后有一个F,因为它看到了内部示例,运行了该示例,并且毫不奇怪地,它失败了。

How do I make rspec not see this nested example as one of the examples it's supposed to run, so that this example completes with a single dot? 我如何使rspec不能将此嵌套示例视为应该运行的示例之一,以便该示例以一个点完成?

(If you're wondering about what the rspec devs themselves do about their tests, it looks like they use cucumber. Do they use cucumber because they couldn't figure this out either? :)) (如果你想知道什么RSpec的开发者自己做一下他们的测试中,它看起来像他们用黄瓜。难道他们用黄瓜,因为他们可能不知道这一点要么?:))

You can use the new sandboxing API (available in 3.2+). 您可以使用新的沙箱API (3.2及更高版本中可用)。

RSpec.configure do |rspec|
  rspec.around do |ex|
    RSpec::Core::Sandbox.sandboxed do |config|
      # re-configure any configuration defined by your extension here
      # before allowing the example to run. The sandbox runs with a fresh
      # config instance, which means any configuration you have set in
      # `rspec-let-and-after-extension` will not apply while the example
      # is running.
      # config.extend MyExtensionModule
      ex.run
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM