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

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

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_block(node) ⇒ Object


49
50
51
52
53
54
55
# File 'lib/rubocop/cop/rspec/let_setup.rb', line 49

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

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