Skip to content

Commit 9e55194

Browse files
authored
Merge pull request #1133 from paydaylight/add-prefer-before-over-setup-cop
Add new RSpec/AvoidSetupHook cop
2 parents e0e109c + e94b463 commit 9e55194

File tree

7 files changed

+121
-0
lines changed

7 files changed

+121
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Add missing documentation for `single_statement_only` style of `RSpec/ImplicitSubject` cop. ([@tejasbubane][])
66
* Fix an exception in `DescribedClass` when accessing a constant on a variable in a spec that is nested in a namespace. ([@rrosenblum][])
77
* Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][])
8+
* Add `RSpec/Rails/AvoidSetupHook cop. ([@paydaylight][])
89

910
## 2.3.0 (2021-04-28)
1011

@@ -618,3 +619,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
618619
[@stephannv]: https://github.com/stephannv
619620
[@Tietew]: https://github.com/Tietew
620621
[@rrosenblum]: https://github.com/rrosenblum
622+
[@paydaylight]: https://github.com/paydaylight

config/default.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,12 @@ RSpec/FactoryBot/FactoryClassName:
759759
VersionChanged: '2.0'
760760
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName
761761

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+
762768
RSpec/Rails/HttpStatus:
763769
Description: Enforces use of symbolic or numeric value to describe HTTP status.
764770
Enabled: true

docs/modules/ROOT/pages/cops.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797

9898
=== Department xref:cops_rspec/rails.adoc[RSpec/Rails]
9999

100+
* xref:cops_rspec/rails.adoc#rspecrails/avoidsetuphook[RSpec/Rails/AvoidSetupHook]
100101
* xref:cops_rspec/rails.adoc#rspecrails/httpstatus[RSpec/Rails/HttpStatus]
101102

102103
// END_COP_LIST

docs/modules/ROOT/pages/cops_rspec/rails.adoc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
= RSpec/Rails
22

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+
336
== RSpec/Rails/HttpStatus
437

538
|===
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

lib/rubocop/cop/rspec_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require_relative 'rspec/factory_bot/create_list'
99
require_relative 'rspec/factory_bot/factory_class_name'
1010

11+
require_relative 'rspec/rails/avoid_setup_hook'
1112
begin
1213
require_relative 'rspec/rails/http_status'
1314
rescue LoadError
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

0 commit comments

Comments
 (0)