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