Skip to content

Commit

Permalink
WIP: Implicitly set responder on partial mocks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
floehopper committed Aug 15, 2022
1 parent ed9c040 commit 3be2e8b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/mocha/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/object_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/unit/mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3be2e8b

Please sign in to comment.