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}` is already defined.'.freeze

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



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

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

  _describe, _args, body = *node

  find_duplicates(body) do |duplicate, name|
    add_offense(duplicate, :expression, format(MSG, name: name))
  end
end