バックトレースから行を除外する
障害の診断時にノイズを減らすために、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 example, 1 failure" が含まれるべきです
かつ、出力に "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 example, 1 failure" が含まれるべきです
かつ、出力に "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 example, 1 failure" が含まれるべきです
かつ、出力に "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 example, 1 failure" という文字列が含まれているべきです。
また、出力には "spec/support/custom_helper.rb:2:in `assert_baz'" という文字列が含まれているべきです。
さらに、出力には "lib/rspec/expectations" という文字列が含まれているべきです。
そして、出力には "lib/rspec/core" という文字列が含まれているべきです。
filter_gems_from_backtrace
を使用して指定されたジェムをフィルタリングする
my_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
の出力には "vendor/my_gem-1.2.3/lib/my_gem.rb:4:in `do_amazing_things!'" という文字列が含まれているべきです。
しかし、rspec --require spec_helper
の出力には "vendor/my_gem-1.2.3/lib/my_gem.rb:4:in `do_amazing_things!'" という文字列が含まれていないべきです。