Class: RuboCop::Cop::RSpec::MessageExpectation

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
ConfigurableEnforcedStyle, RSpec::SpecOnly
Defined in:
lib/rubocop/cop/rspec/message_expectation.rb

Overview

Checks for consistent message expectation style.

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

Examples:

‘EnforcedStyle: allow`


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

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

‘EnforcedStyle: expect`


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

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

Constant Summary collapse

MSG =
'Prefer `%s` for setting message expectations.'.freeze
SUPPORTED_STYLES =
%w(allow expect).freeze

Constants included from RSpec::SpecOnly

RSpec::SpecOnly::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods included from RSpec::SpecOnly

#relevant_file?

Instance Method Details

#on_send(node) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/rspec/message_expectation.rb', line 40

def on_send(node)
  message_expectation(node) do |match|
    return correct_style_detected if preferred_style?(match)

    add_offense(match, :selector, MSG % style) do
      opposite_style_detected
    end
  end
end