File tree Expand file tree Collapse file tree 7 files changed +121
-0
lines changed
spec/rubocop/cop/rspec/rails Expand file tree Collapse file tree 7 files changed +121
-0
lines changed Original file line number Diff line number Diff line change 5
5
* Add missing documentation for ` single_statement_only ` style of ` RSpec/ImplicitSubject ` cop. ([ @tejasbubane ] [ ] )
6
6
* Fix an exception in ` DescribedClass ` when accessing a constant on a variable in a spec that is nested in a namespace. ([ @rrosenblum ] [ ] )
7
7
* Add new ` RSpec/IdenticalEqualityAssertion ` cop. ([ @tejasbubane ] [ ] )
8
+ * Add `RSpec/Rails/AvoidSetupHook cop. ([ @paydaylight ] [ ] )
8
9
9
10
## 2.3.0 (2021-04-28)
10
11
@@ -618,3 +619,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
618
619
[ @stephannv ] : https://github.com/stephannv
619
620
[ @Tietew ] : https://github.com/Tietew
620
621
[ @rrosenblum ] : https://github.com/rrosenblum
622
+ [ @paydaylight ] : https://github.com/paydaylight
Original file line number Diff line number Diff line change @@ -759,6 +759,12 @@ RSpec/FactoryBot/FactoryClassName:
759
759
VersionChanged : ' 2.0'
760
760
StyleGuide : https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName
761
761
762
+ RSpec/Rails/AvoidSetupHook :
763
+ Description : Checks that tests use RSpec `before` hook over Rails `setup` method.
764
+ Enabled : pending
765
+ VersionAdded : ' 2.4'
766
+ StyleGuide : https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/AvoidSetupHook
767
+
762
768
RSpec/Rails/HttpStatus :
763
769
Description : Enforces use of symbolic or numeric value to describe HTTP status.
764
770
Enabled : true
Original file line number Diff line number Diff line change 97
97
98
98
=== Department xref:cops_rspec/rails.adoc[RSpec/Rails]
99
99
100
+ * xref:cops_rspec/rails.adoc#rspecrails/avoidsetuphook[RSpec/Rails/AvoidSetupHook]
100
101
* xref:cops_rspec/rails.adoc#rspecrails/httpstatus[RSpec/Rails/HttpStatus]
101
102
102
103
// END_COP_LIST
Original file line number Diff line number Diff line change 1
1
= RSpec/Rails
2
2
3
+ == RSpec/Rails/AvoidSetupHook
4
+
5
+ |===
6
+ | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
7
+
8
+ | Pending
9
+ | Yes
10
+ | Yes
11
+ | 2.4
12
+ | -
13
+ |===
14
+
15
+ Checks that tests use RSpec `before` hook over Rails `setup` method.
16
+
17
+ === Examples
18
+
19
+ [source,ruby]
20
+ ----
21
+ # bad
22
+ setup do
23
+ allow(foo).to receive(:bar)
24
+ end
25
+
26
+ # good
27
+ before do
28
+ allow(foo).to receive(:bar)
29
+ end
30
+ ----
31
+
32
+ === References
33
+
34
+ * https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/AvoidSetupHook
35
+
3
36
== RSpec/Rails/HttpStatus
4
37
5
38
|===
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ module Rails
7
+ # Checks that tests use RSpec `before` hook over Rails `setup` method.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # setup do
13
+ # allow(foo).to receive(:bar)
14
+ # end
15
+ #
16
+ # # good
17
+ # before do
18
+ # allow(foo).to receive(:bar)
19
+ # end
20
+ #
21
+ class AvoidSetupHook < Base
22
+ extend AutoCorrector
23
+
24
+ MSG = 'Use `before` instead of `setup`.'
25
+
26
+ # @!method setup_call(node)
27
+ def_node_matcher :setup_call , <<-PATTERN
28
+ (block
29
+ $(send nil? :setup)
30
+ (args) _)
31
+ PATTERN
32
+
33
+ def on_block ( node )
34
+ setup_call ( node ) do |setup |
35
+ add_offense ( node ) do |corrector |
36
+ corrector . replace setup , 'before'
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
Original file line number Diff line number Diff line change 8
8
require_relative 'rspec/factory_bot/create_list'
9
9
require_relative 'rspec/factory_bot/factory_class_name'
10
10
11
+ require_relative 'rspec/rails/avoid_setup_hook'
11
12
begin
12
13
require_relative 'rspec/rails/http_status'
13
14
rescue LoadError
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ RSpec . describe RuboCop ::Cop ::RSpec ::Rails ::AvoidSetupHook do
4
+ it 'registers an offense for `setup`' do
5
+ expect_offense ( <<~RUBY )
6
+ setup do
7
+ ^^^^^^^^ Use `before` instead of `setup`.
8
+ allow(foo).to receive(:bar)
9
+ end
10
+ RUBY
11
+
12
+ expect_correction ( <<~RUBY )
13
+ before do
14
+ allow(foo).to receive(:bar)
15
+ end
16
+ RUBY
17
+ end
18
+
19
+ it 'does not register an offense for `before`' do
20
+ expect_no_offenses ( <<~RUBY )
21
+ before do
22
+ allow(foo).to receive(:bar)
23
+ end
24
+ RUBY
25
+ end
26
+
27
+ it 'does not register an offense for an unrelated `setup` call' do
28
+ expect_no_offenses ( <<~RUBY )
29
+ navigation.setup do
30
+ direction 'to infinity!'
31
+ end
32
+ RUBY
33
+ end
34
+ end
You can’t perform that action at this time.
0 commit comments