Class: RuboCop::Cop::RSpec::MessageChain

Inherits:
RuboCop::Cop show all
Includes:
RSpec::SpecOnly
Defined in:
lib/rubocop/cop/rspec/message_chain.rb

Overview

Check that chains of messages are not being stubbed.

Examples:

# bad
allow(foo).to receive_message_chain(:bar, :baz).and_return(42)

# better
thing = Thing.new(baz: 42)
allow(foo).to receive(bar: thing)

Constant Summary collapse

MESSAGE =
'Avoid stubbing using `%<method>s`'.freeze
MESSAGE_CHAIN_METHODS =
[
  :receive_message_chain,
  :stub_chain
].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



24
25
26
27
28
29
# File 'lib/rubocop/cop/rspec/message_chain.rb', line 24

def on_send(node)
  _receiver, method_name, *_args = *node
  return unless MESSAGE_CHAIN_METHODS.include?(method_name)

  add_offense(node, :selector, MESSAGE % { method: method_name })
end