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

Inherits:
Cop
  • Object
show all
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.'.freeze

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/rspec/single_argument_message_chain.rb', line 37

def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.loc.selector, replacement(node.method_name))
    message_chain(node) do |arg|
      autocorrect_hash_arg(corrector, arg) if single_key_hash?(arg)
    end
  end
end

#on_send(node) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/rubocop/cop/rspec/single_argument_message_chain.rb', line 27

def on_send(node)
  message_chain(node) do |arg|
    return if arg.to_s.include?('.')

    return if arg.hash_type? && !single_key_hash?(arg)

    add_offense(node, location: :selector)
  end
end