Class: RuboCop::Cop::RSpecRails::AvoidSetupHook

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb

Overview

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

Examples:

# bad
setup do
  allow(foo).to receive(:bar)
end

# good
before do
  allow(foo).to receive(:bar)
end

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



31
32
33
34
35
36
37
# File 'lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb', line 31

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  setup_call(node) do |setup|
    add_offense(node) do |corrector|
      corrector.replace setup, 'before'
    end
  end
end

#setup_call(node) ⇒ Object



25
26
27
28
29
# File 'lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb', line 25

def_node_matcher :setup_call, <<~PATTERN
  (block
    $(send nil? :setup)
    (args) _)
PATTERN