Class: RuboCop::Cop::RSpec::SingleArgumentMessageChain

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/single_argument_message_chain.rb

Overview

Checks that chains of messages contain more than one element.

Examples:

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

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

# also good
allow(foo).to receive(:bar, :baz)
allow(foo).to receive("bar.baz")

Constant Summary collapse

MSG =
'Use `%<recommended>s` instead of calling '\
'`%<called>s` with a single argument.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_send(node) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rspec/single_argument_message_chain.rb', line 31

def on_send(node)
  message_chain(node) do |arg|
    return if valid_usage?(arg)

    method = node.method_name
    msg = format(MSG, recommended: replacement(method), called: method)

    add_offense(node.loc.selector, message: msg) do |corrector|
      autocorrect(corrector, node, method, arg)
    end
  end
end