首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如果我正在测试一个rspec扩展,如何抑制测试失败的结果?

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

Stack Overflow用户
提问于 2015-07-28 05:52:34
回答 1查看 143关注 0票数 1

我正试图为rspec的扩展编写规范。

这就是我试着测试的要点:

代码语言:javascript
代码运行次数:0
运行
复制
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

其中一个重要的行为是,如果运行该示例失败,清理代码仍然运行。因此,我通过记录调用顺序并从示例中引发异常来测试这一点。

问题是,当我运行它时,它将这个块看作第二个示例,然后在出现错误时失败:

代码语言:javascript
代码运行次数:0
运行
复制
.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 #  

正如您所看到的,输出确实有一个点,所以实际的例子通过了。但是还有一个F,因为它看到了内部的例子,运行它,毫不奇怪,其中一个失败了。

如何使rspec不将这个嵌套的示例视为它应该运行的示例之一,从而使这个示例用一个点完成?

(如果您想知道rspec开发人员自己对他们的测试做了什么,看起来他们使用的是黄瓜。他们用黄瓜是因为他们也搞不懂这个吗?:)

EN

回答 1

Stack Overflow用户

发布于 2015-07-28 07:03:53

您可以使用新的沙箱API (在3.2+中可用)。

代码语言:javascript
代码运行次数:0
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31668319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档