Skip to content

Commit efe7c53

Browse files
committed
Merge pull request #1458 from rspec/rails-exclusion-filters
Rails exclusion filters
2 parents 819a697 + 8118b91 commit efe7c53

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

features/.nav

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Changelog.md
77
- RailsVersions.md (Rails versions)
88
- directory_structure.feature
9+
- backtrace_filtering.feature
910
- model_specs:
1011
- transactional_examples.feature
1112
- mocks:

features/backtrace_filtering.feature

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Feature: backtrace filtering
2+
3+
The following configuration setting will filter out lines in backtraces that come from Rails gems in order to reduce the noise in test failure output:
4+
5+
```ruby
6+
RSpec.configure do |config|
7+
config.filter_rails_from_backtrace!
8+
end
9+
```
10+
11+
`rspec` will always show the full backtrace output when run with the `--backtrace` commandline option.
12+
13+
Background: Using `filter_rails_from_backtrace!`
14+
Given a file named "spec/failing_spec.rb" with:
15+
"""ruby
16+
require "rails_helper"
17+
18+
RSpec.configure do |config|
19+
config.filter_rails_from_backtrace!
20+
end
21+
22+
RSpec.describe "Controller", :type => :controller do
23+
controller do
24+
def index
25+
raise "Something went wrong."
26+
end
27+
end
28+
29+
describe "GET index" do
30+
it "raises an error" do
31+
get :index
32+
end
33+
end
34+
end
35+
"""
36+
37+
Scenario: Using the bare `rspec` command
38+
When I run `rspec`
39+
Then the output should contain "1 example, 1 failure"
40+
And the output should not contain "activesupport"
41+
42+
Scenario: Using `rspec --backtrace`
43+
When I run `rspec --backtrace`
44+
Then the output should contain "1 example, 1 failure"
45+
And the output should contain "activesupport"

lib/generators/rspec/install/templates/spec/rails_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@
5858
# The different available types are documented in the features, such as in
5959
# https://relishapp.com/rspec/rspec-rails/docs
6060
config.infer_spec_type_from_file_location!
61+
62+
# Filter lines from Rails gems in backtraces.
63+
config.filter_rails_from_backtrace!
64+
# arbitrary gems may also be filtered via:
65+
# config.filter_gems_from_backtrace("gem name")
6166
end

lib/rspec/rails/configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ def infer_spec_type_from_file_location!
8989
end
9090
end
9191
end
92+
93+
# Adds exclusion filters for gems included with Rails
94+
def filter_rails_from_backtrace!
95+
filter_gems_from_backtrace "actionmailer", "actionpack", "actionview"
96+
filter_gems_from_backtrace "activemodel", "activerecord",
97+
"activesupport", "activejob"
98+
end
9299
end
93100

94101
config.include RSpec::Rails::ControllerExampleGroup, :type => :controller

spec/generators/rspec/install/install_generator_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def use_transactional_fixtures
3131
match(/config\.use_transactional_fixtures/m)
3232
end
3333

34+
def filter_rails_from_backtrace
35+
match(/config\.filter_rails_from_backtrace!/m)
36+
end
37+
3438
setup_default_destination
3539

3640
let(:rails_helper) { content_for('spec/rails_helper.rb') }
@@ -69,6 +73,12 @@ def use_transactional_fixtures
6973
expect(rails_helper).to use_transactional_fixtures
7074
end
7175

76+
specify "excluding rails gems from the backtrace" do
77+
run_generator
78+
79+
expect(rails_helper).to filter_rails_from_backtrace
80+
end
81+
7282
if RSpec::Rails::FeatureCheck.can_maintain_test_schema?
7383
specify "checking for maintaining the schema" do
7484
run_generator

spec/rspec/rails/configuration_spec.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require 'rspec/support/spec/in_sub_process'
33

44
RSpec.describe "Configuration" do
5-
65
include RSpec::Support::InSubProcess
76

87
subject(:config) { RSpec::Core::Configuration.new }
@@ -99,6 +98,24 @@
9998
end
10099
end
101100

101+
specify "#filter_rails_from_backtrace! adds exclusion patterns for rails gems" do
102+
config.filter_rails_from_backtrace!
103+
104+
gems = %w[
105+
actionmailer
106+
actionpack
107+
actionview
108+
activemodel
109+
activerecord
110+
activesupport
111+
activejob
112+
]
113+
exclusions = config.backtrace_exclusion_patterns.map(&:to_s)
114+
aggregate_failures do
115+
gems.each { |gem| expect(exclusions).to include(/#{gem}/) }
116+
end
117+
end
118+
102119
describe "#infer_spec_type_from_file_location!" do
103120
def in_inferring_type_from_location_environment
104121
in_sub_process do
@@ -229,5 +246,4 @@ def in_inferring_type_from_location_environment
229246
expect(group.new).to be_a(RSpec::Rails::MailerExampleGroup)
230247
end
231248
end
232-
233249
end

0 commit comments

Comments
 (0)