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

Inherits:
Cop
  • 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.'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



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

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

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