From 3be2e8b25c3975dc03938183d9d598f152303eaf Mon Sep 17 00:00:00 2001 From: James Mead Date: Mon, 15 Aug 2022 15:53:02 +0100 Subject: [PATCH] WIP: Implicitly set responder on partial mocks This relates to #149 and #531. All the tests seem to pass, but it would be worth doing a bit more thinking about the change in Mock#check_responder_responds_to where we now set include_all to true for the call to Object#respond_to?. Also it's only really worth doing this if the investigation in #531 means that it's definitely worthwhile. It would probably also be worth doing some spiking on the responder-related solution proposed in #149. --- lib/mocha/class_methods.rb | 2 +- lib/mocha/mock.rb | 2 +- lib/mocha/object_methods.rb | 2 +- test/unit/mock_test.rb | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/mocha/class_methods.rb b/lib/mocha/class_methods.rb index 5c8404c0c..14089d901 100644 --- a/lib/mocha/class_methods.rb +++ b/lib/mocha/class_methods.rb @@ -12,7 +12,7 @@ def initialize(klass) def mocha(instantiate = true) if instantiate - @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object) + @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object).responds_like_instance_of(@stubba_object) else defined?(@mocha) ? @mocha : nil end diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb index f82121e41..af78062ea 100644 --- a/lib/mocha/mock.rb +++ b/lib/mocha/mock.rb @@ -385,7 +385,7 @@ def raise_unexpected_invocation_error(invocation, matching_expectation) end def check_responder_responds_to(symbol) - if @responder && !@responder.respond_to?(symbol) # rubocop:disable Style/GuardClause + if @responder && !@responder.respond_to?(symbol, true) # rubocop:disable Style/GuardClause raise NoMethodError, "undefined method `#{symbol}' for #{mocha_inspect} which responds like #{@responder.mocha_inspect}" end end diff --git a/lib/mocha/object_methods.rb b/lib/mocha/object_methods.rb index 4e4e59673..045a33174 100644 --- a/lib/mocha/object_methods.rb +++ b/lib/mocha/object_methods.rb @@ -14,7 +14,7 @@ module ObjectMethods # @private def mocha(instantiate = true) if instantiate - @mocha ||= Mocha::Mockery.instance.mock_impersonating(self) + @mocha ||= Mocha::Mockery.instance.mock_impersonating(self).responds_like(self) else defined?(@mocha) ? @mocha : nil end diff --git a/test/unit/mock_test.rb b/test/unit/mock_test.rb index ed31db5ce..1f4ec3a0e 100644 --- a/test/unit/mock_test.rb +++ b/test/unit/mock_test.rb @@ -299,7 +299,7 @@ def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_l def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method instance = Class.new do - define_method(:respond_to?) { |_symbol| true } + define_method(:respond_to?) { |_symbol, _include_all = false| true } end.new mock = build_mock mock.responds_like(instance) @@ -309,7 +309,7 @@ def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_m def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method instance = Class.new do - define_method(:respond_to?) { |_symbol| false } + define_method(:respond_to?) { |_symbol, _include_all = false| false } define_method(:mocha_inspect) { 'mocha_inspect' } end.new mock = build_mock @@ -320,7 +320,7 @@ def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_m def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder instance = Class.new do - define_method(:respond_to?) { |_symbol| false } + define_method(:respond_to?) { |_symbol, _include_all = false| false } define_method(:mocha_inspect) { 'mocha_inspect' } end.new mock = build_mock