Class: RuboCop::Cop::RSpec::MessageSpies

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

Overview

Checks that message expectations are set using spies.

This cop can be configured in your configuration using the ‘EnforcedStyle` option and supports `–auto-gen-config`.

Examples:

‘EnforcedStyle: have_received`


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

# good
expect(foo).to have_received(:bar)

‘EnforcedStyle: receive`


# bad
expect(foo).to have_received(:bar)

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

Constant Summary collapse

MSG_RECEIVE =
'Prefer `receive` for setting message expectations.'
MSG_HAVE_RECEIVED =
'Prefer `have_received` for setting message '\
'expectations. Setup `%<source>s` as a spy using '\
'`allow` or `instance_spy`.'
SUPPORTED_STYLES =
%w[have_received receive].freeze

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_send(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/rspec/message_spies.rb', line 46

def on_send(node)
  receive_message_matcher(node) do |receiver, message_matcher|
    return correct_style_detected if preferred_style?(message_matcher)

    add_offense(
      message_matcher,
      location: :selector,
      message: error_message(receiver)
    ) { opposite_style_detected }
  end
end