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 20, 2022
1 parent 1ade384 commit 446f107
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 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 @@ -381,7 +381,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

0 comments on commit 446f107

Please sign in to comment.