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_pattern, #send_pattern

Methods included from RSpec::Language

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

Instance Method Details

#on_block(node) ⇒ Object


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

def on_block(node)
  setup_call(node) do |setup|
    add_offense(node) do |corrector|
      corrector.replace setup, 'before'
    end
  end
end

#setup_call(node) ⇒ Object


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

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