diff --git a/lib/rubocop/cop/rspec/stubbed_mock.rb b/lib/rubocop/cop/rspec/stubbed_mock.rb index a84d4133d..2646e0b71 100644 --- a/lib/rubocop/cop/rspec/stubbed_mock.rb +++ b/lib/rubocop/cop/rspec/stubbed_mock.rb @@ -30,51 +30,49 @@ class StubbedMock < Base :and_call_original :and_wrap_original } PATTERN - def self.expectation_with(matcher) - <<~PATTERN - (send - $(send nil? $#{Expectations::ALL.node_pattern_union} ...) - :to #{matcher} - ) - PATTERN - end + def_node_matcher :expectation, <<~PATTERN + (send + $(send nil? $#{Expectations::ALL.node_pattern_union} ...) + :to $_) + PATTERN - def_node_matcher :expectation_with_configured_response, - expectation_with(<<~PATTERN) - (send #message_expectation? #configured_response? _) - PATTERN + def_node_matcher :matcher_with_configured_response, <<~PATTERN + (send #message_expectation? #configured_response? _) + PATTERN - def_node_matcher :expectation_with_return_block, - expectation_with(<<~PATTERN) - (block #message_expectation? args _) - PATTERN + def_node_matcher :matcher_with_return_block, <<~PATTERN + (block #message_expectation? args _) + PATTERN - def_node_matcher :expectation_with_blockpass_or_hash, - expectation_with(<<~PATTERN) - { - (send nil? { :receive :receive_message_chain } ... block_pass) - (send (send nil? :receive ...) :with ... block_pass) - (send nil? :receive_messages hash) - (send nil? :receive_message_chain ... hash) - } - PATTERN + def_node_matcher :matcher_with_blockpass_or_hash, <<~PATTERN + { + (send nil? { :receive :receive_message_chain } ... block_pass) + (send (send nil? :receive ...) :with ... block_pass) + (send nil? :receive_messages hash) + (send nil? :receive_message_chain ... hash) + } + PATTERN def on_send(node) - expectation_with_configured_response(node) do |match, method_name| - add_offense(match, message: msg(method_name)) + expectation(node, &method(:on_expectation)) + end + + private + + def on_expectation(expectation, method_name, matcher) + matcher_with_configured_response(matcher) do + add_offense(expectation, message: msg(method_name)) end - expectation_with_return_block(node) do |match, method_name| - add_offense(match, message: msg(method_name)) + matcher_with_return_block(matcher) do + add_offense(expectation, message: msg(method_name)) end - expectation_with_blockpass_or_hash(node) do |match, method_name| - add_offense(match, message: msg(method_name)) + matcher_with_blockpass_or_hash(matcher) do + add_offense(expectation, message: msg(method_name)) end end - private - def msg(method_name) format(MSG, method_name: method_name,