Skip to content

Commit

Permalink
Merge pull request #1133 from paydaylight/add-prefer-before-over-setu…
Browse files Browse the repository at this point in the history
…p-cop

Add new RSpec/AvoidSetupHook cop
  • Loading branch information
pirj authored May 25, 2021
2 parents e0e109c + e94b463 commit 9e55194
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add missing documentation for `single_statement_only` style of `RSpec/ImplicitSubject` cop. ([@tejasbubane][])
* Fix an exception in `DescribedClass` when accessing a constant on a variable in a spec that is nested in a namespace. ([@rrosenblum][])
* Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][])
* Add `RSpec/Rails/AvoidSetupHook cop. ([@paydaylight][])

## 2.3.0 (2021-04-28)

Expand Down Expand Up @@ -618,3 +619,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@stephannv]: https://github.com/stephannv
[@Tietew]: https://github.com/Tietew
[@rrosenblum]: https://github.com/rrosenblum
[@paydaylight]: https://github.com/paydaylight
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,12 @@ RSpec/FactoryBot/FactoryClassName:
VersionChanged: '2.0'
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName

RSpec/Rails/AvoidSetupHook:
Description: Checks that tests use RSpec `before` hook over Rails `setup` method.
Enabled: pending
VersionAdded: '2.4'
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/AvoidSetupHook

RSpec/Rails/HttpStatus:
Description: Enforces use of symbolic or numeric value to describe HTTP status.
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@

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

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

// END_COP_LIST
33 changes: 33 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec/rails.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
= RSpec/Rails

== RSpec/Rails/AvoidSetupHook

|===
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Pending
| Yes
| Yes
| 2.4
| -
|===

Checks that tests use RSpec `before` hook over Rails `setup` method.

=== Examples

[source,ruby]
----
# bad
setup do
allow(foo).to receive(:bar)
end
# good
before do
allow(foo).to receive(:bar)
end
----

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/AvoidSetupHook

== RSpec/Rails/HttpStatus

|===
Expand Down
44 changes: 44 additions & 0 deletions lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
module Rails
# Checks that tests use RSpec `before` hook over Rails `setup` method.
#
# @example
#
# # bad
# setup do
# allow(foo).to receive(:bar)
# end
#
# # good
# before do
# allow(foo).to receive(:bar)
# end
#
class AvoidSetupHook < Base
extend AutoCorrector

MSG = 'Use `before` instead of `setup`.'

# @!method setup_call(node)
def_node_matcher :setup_call, <<-PATTERN
(block
$(send nil? :setup)
(args) _)
PATTERN

def on_block(node)
setup_call(node) do |setup|
add_offense(node) do |corrector|
corrector.replace setup, 'before'
end
end
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative 'rspec/factory_bot/create_list'
require_relative 'rspec/factory_bot/factory_class_name'

require_relative 'rspec/rails/avoid_setup_hook'
begin
require_relative 'rspec/rails/http_status'
rescue LoadError
Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/cop/rspec/rails/avoid_setup_hook_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::Rails::AvoidSetupHook do
it 'registers an offense for `setup`' do
expect_offense(<<~RUBY)
setup do
^^^^^^^^ Use `before` instead of `setup`.
allow(foo).to receive(:bar)
end
RUBY

expect_correction(<<~RUBY)
before do
allow(foo).to receive(:bar)
end
RUBY
end

it 'does not register an offense for `before`' do
expect_no_offenses(<<~RUBY)
before do
allow(foo).to receive(:bar)
end
RUBY
end

it 'does not register an offense for an unrelated `setup` call' do
expect_no_offenses(<<~RUBY)
navigation.setup do
direction 'to infinity!'
end
RUBY
end
end

0 comments on commit 9e55194

Please sign in to comment.