File tree Expand file tree Collapse file tree 6 files changed +86
-2
lines changed
generators/rspec/install/templates/spec Expand file tree Collapse file tree 6 files changed +86
-2
lines changed Original file line number Diff line number Diff line change 6
6
- Changelog.md
7
7
- RailsVersions.md (Rails versions)
8
8
- directory_structure.feature
9
+ - backtrace_filtering.feature
9
10
- model_specs:
10
11
- transactional_examples.feature
11
12
- mocks:
Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change 58
58
# The different available types are documented in the features, such as in
59
59
# https://relishapp.com/rspec/rspec-rails/docs
60
60
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")
61
66
end
Original file line number Diff line number Diff line change @@ -89,6 +89,13 @@ def infer_spec_type_from_file_location!
89
89
end
90
90
end
91
91
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
92
99
end
93
100
94
101
config . include RSpec ::Rails ::ControllerExampleGroup , :type => :controller
Original file line number Diff line number Diff line change @@ -31,6 +31,10 @@ def use_transactional_fixtures
31
31
match ( /config\. use_transactional_fixtures/m )
32
32
end
33
33
34
+ def filter_rails_from_backtrace
35
+ match ( /config\. filter_rails_from_backtrace!/m )
36
+ end
37
+
34
38
setup_default_destination
35
39
36
40
let ( :rails_helper ) { content_for ( 'spec/rails_helper.rb' ) }
@@ -69,6 +73,12 @@ def use_transactional_fixtures
69
73
expect ( rails_helper ) . to use_transactional_fixtures
70
74
end
71
75
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
+
72
82
if RSpec ::Rails ::FeatureCheck . can_maintain_test_schema?
73
83
specify "checking for maintaining the schema" do
74
84
run_generator
Original file line number Diff line number Diff line change 2
2
require 'rspec/support/spec/in_sub_process'
3
3
4
4
RSpec . describe "Configuration" do
5
-
6
5
include RSpec ::Support ::InSubProcess
7
6
8
7
subject ( :config ) { RSpec ::Core ::Configuration . new }
99
98
end
100
99
end
101
100
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
+
102
119
describe "#infer_spec_type_from_file_location!" do
103
120
def in_inferring_type_from_location_environment
104
121
in_sub_process do
@@ -229,5 +246,4 @@ def in_inferring_type_from_location_environment
229
246
expect ( group . new ) . to be_a ( RSpec ::Rails ::MailerExampleGroup )
230
247
end
231
248
end
232
-
233
249
end
You can’t perform that action at this time.
0 commit comments