class RuboCop::Cop::RSpec::SingleArgumentMessageChain
Checks that chains of messages contain more than one element.
@example
# 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")
Constants
- MSG
Public Instance Methods
autocorrect(node)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
autocorrect_hash_arg(corrector, arg)
click to toggle source
# File lib/rubocop/cop/rspec/single_argument_message_chain.rb, line 48 def autocorrect_hash_arg(corrector, arg) key, value = *arg.children.first corrector.replace(arg.loc.expression, key_to_arg(key)) corrector.insert_after(arg.parent.loc.end, ".and_return(#{value.source})") end
key_to_arg(node)
click to toggle source
# File lib/rubocop/cop/rspec/single_argument_message_chain.rb, line 56 def key_to_arg(node) key, = *node node.sym_type? ? ":#{key}" : node.source end
message(node)
click to toggle source
# File lib/rubocop/cop/rspec/single_argument_message_chain.rb, line 65 def message(node) method = node.method_name format(MSG, recommended: replacement(method), called: method) end
replacement(method)
click to toggle source
# File lib/rubocop/cop/rspec/single_argument_message_chain.rb, line 61 def replacement(method) method.equal?(:receive_message_chain) ? 'receive' : 'stub' end