Class: RuboCop::Cop::RSpec::LetNotInContext

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

Overview

Requires ‘let`/`let!` to be declared at the top of the example group, never inside a `context`. All setup objects live at the top level.

Examples:

# bad
context 'when disabled' do
  let(:user) { create(:user) }
end

# good
let(:user) { create(:user) }

context 'when disabled' do
  before { user.disable }
end

Constant Summary collapse

MSG =
'Declare `let` at the top level, not inside a `context`.'
LET_METHODS =
%i[let let!].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



29
30
31
32
33
34
# File 'lib/rubocop/cop/rspec/let_not_in_context.rb', line 29

def on_send(node)
  return unless LET_METHODS.include?(node.method_name) && node.receiver.nil?
  return unless node.each_ancestor(:block).any? { |ancestor| context_block?(ancestor) }

  add_offense(node.loc.selector)
end