Class: RuboCop::Cop::RSpec::Rails::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

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



32
33
34
35
36
37
38
# File 'lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb', line 32

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



26
27
28
29
30
# File 'lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb', line 26

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