Class: RuboCop::Cop::RSpec::OverwritingSetup

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/overwriting_setup.rb

Overview

Checks if there is a let/subject that overwrites an existing one.

Examples:

# bad
let(:foo) { bar }
let(:foo) { baz }

subject(:foo) { bar }
let(:foo) { baz }

let(:foo) { bar }
let!(:foo) { baz }

# good
subject(:test) { something }
let(:foo) { bar }
let(:baz) { baz }
let!(:other) { other }

Constant Summary collapse

MSG =
'`%<name>s` is already defined.'

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

#first_argument_name(node) ⇒ Object


31
# File 'lib/rubocop/cop/rspec/overwriting_setup.rb', line 31

def_node_matcher :first_argument_name, '(send _ _ ({str sym} $_))'

#on_block(node) ⇒ Object


33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rspec/overwriting_setup.rb', line 33

def on_block(node)
  return unless example_group_with_body?(node)

  find_duplicates(node.body) do |duplicate, name|
    add_offense(
      duplicate,
      message: format(MSG, name: name)
    )
  end
end

#setup?(node) ⇒ Object


28
# File 'lib/rubocop/cop/rspec/overwriting_setup.rb', line 28

def_node_matcher :setup?, block_pattern('{#Helpers.all #Subjects.all}')