Class: RuboCop::Cop::RSpec::ReceiveNever

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

Overview

Prefer ‘not_to receive(…)` over `receive(…).never`.

Examples:


# bad
expect(foo).to receive(:bar).never

# good
expect(foo).not_to receive(:bar)

Constant Summary collapse

MSG =
'Use `not_to receive` instead of `never`.'

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

#autocorrect(node) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/rubocop/cop/rspec/receive_never.rb', line 30

def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.parent.loc.selector, 'not_to')
    range = node.loc.dot.with(end_pos: node.loc.selector.end_pos)
    corrector.remove(range)
  end
end

#on_send(node) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/rspec/receive_never.rb', line 21

def on_send(node)
  return unless node.method_name == :never && method_on_stub?(node)

  add_offense(
    node,
    location: :selector
  )
end