从回溯中排除行

为了减少在诊断错误时产生的噪音,RSpec 可以排除属于某些 gem 或匹配给定模式的行。

如果要过滤掉属于特定 gem 的回溯行,可以使用`config.filter_gems_from_backtrace`,如下所示

  config.filter_gems_from_backtrace "ignored_gem", "another_ignored_gem",

为了更好地控制要忽略哪些行,可以使用`backtrace_exclusion_patterns` 选项来替换默认排除模式,或附加您自己的模式,例如

  config.backtrace_exclusion_patterns = [/first pattern/, /second pattern/]
  config.backtrace_exclusion_patterns << /another pattern/

默认排除模式是

  /\/lib\d*\/ruby\//,
  /org\/jruby\//,
  /bin\//,
  /lib\/rspec\/(core|expectations|matchers|mocks)/

此外,`rspec` 可以使用`--backtrace` 选项运行,以完全跳过回溯清理。

使用默认`backtrace_exclusion_patterns`

给定一个名为“spec/failing_spec.rb”的文件,其中包含

RSpec.describe "2 + 2" do
  it "is 5" do
    expect(2+2).to eq(5)
  end
end

我运行`rspec` 时

那么输出应包含“1 个示例,1 个失败”。

并且输出不应包含“lib/rspec/expectations”。

替换`backtrace_exclusion_patterns`

给定一个名为“spec/spec_helper.rb”的文件,其中包含

RSpec.configure do |config|
  config.backtrace_exclusion_patterns = [
    /spec_helper/
  ]
end

以及一个名为“spec/example_spec.rb”的文件,其中包含

require 'spec_helper'
RSpec.describe "foo" do
  it "returns baz" do
    expect("foo").to eq("baz")
  end
end

我运行`rspec` 时

那么输出应包含“1 个示例,1 个失败”。

并且输出应包含“lib/rspec/expectations”。

附加到`backtrace_exclusion_patterns`

给定一个名为“spec/support/assert_baz.rb”的文件,其中包含

require "support/really_assert_baz"

def assert_baz(arg)
  really_assert_baz(arg)
end

以及一个名为“spec/support/really_assert_baz.rb”的文件,其中包含

def really_assert_baz(arg)
  expect(arg).to eq("baz")
end

以及一个名为“spec/example_spec.rb”的文件,其中包含

require "support/assert_baz"
RSpec.configure do |config|
  config.backtrace_exclusion_patterns << /really/
end

RSpec.describe "bar" do
  it "is baz" do
    assert_baz("bar")
  end
end

我运行`rspec` 时

那么输出应包含“1 个示例,1 个失败”。

并且输出应包含“assert_baz”。

但是输出不应包含“really_assert_baz”。

并且输出不应包含“lib/rspec/expectations”。

使用`--backtrace` 运行`rspec` 会打印未过滤的回溯

给定一个名为“spec/support/custom_helper.rb”的文件,其中包含

def assert_baz(arg)
  expect(arg).to eq("baz")
end

以及一个名为“spec/example_spec.rb”的文件,其中包含

require "support/custom_helper"

RSpec.configure do |config|
  config.backtrace_exclusion_patterns << /custom_helper/
end

RSpec.describe "bar" do
  it "is baz" do
    assert_baz("bar")
  end
end

我运行`rspec --backtrace` 时

那么输出应包含“1 个示例,1 个失败”。

并且输出应包含%R{spec/support/custom_helper.rb:2:in (`|‘Object#|`)assert_baz’}

并且输出应包含“lib/rspec/expectations”。

并且输出应包含“lib/rspec/core”。

使用`filter_gems_from_backtrace` 过滤命名 gem

给定一个名为“my_gem”的供应商 gem,其中包含一个名为“lib/my_gem.rb”的文件,其中包含

class MyGem
  def self.do_amazing_things!
    # intentional bug to trigger an exception
    impossible_math = 10 / 0
    "10 div 0 is: #{impossible_math}"
  end
end

以及一个名为“spec/use_my_gem_spec.rb”的文件,其中包含

require 'my_gem'

RSpec.describe "Using my_gem" do
  it 'does amazing things' do
    expect(MyGem.do_amazing_things!).to include("10 div 0 is")
  end
end

以及一个名为“spec/spec_helper.rb”的文件,其中包含

RSpec.configure do |config|
  config.filter_gems_from_backtrace "my_gem"
end

那么来自`rspec` 的输出应包含%R{vendor/my_gem-1.2.3/lib/my_gem.rb:4:in (`|‘MyGem.)do_amazing_things!’}

但是来自`rspec --require spec_helper` 的输出不应包含%R{vendor/my_gem-1.2.3/lib/my_gem.rb:4:in (`|‘MyGem.)do_amazing_things!’}.