Class: RuboCop::Cop::RSpec::LetSetup

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/let_setup.rb

Overview

Checks unreferenced ‘let!` calls being used for test setup.

Examples:

# Bad
let!(:my_widget) { create(:widget) }

it 'counts widgets' do
  expect(Widget.count).to eq(1)
end

# Good
it 'counts widgets' do
  create(:widget)
  expect(Widget.count).to eq(1)
end

# Good
before { create(:widget) }

it 'counts widgets' do
  expect(Widget.count).to eq(1)
end

Constant Summary collapse

MSG =
'Do not use `let!` to setup objects not referenced in tests.'

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



37
38
39
40
41
42
43
# File 'lib/rubocop/cop/rspec/let_setup.rb', line 37

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

  unused_let_bang(node) do |let|
    add_offense(let)
  end
end