Skip to content

Instantly share code, notes, and snippets.

@tdg5
Last active January 11, 2016 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdg5/aa135700491c5863e203 to your computer and use it in GitHub Desktop.
Save tdg5/aa135700491c5863e203 to your computer and use it in GitHub Desktop.
Mocha Expectation#calls_through
module Concerns
module Mocha
module CallsThrough
def calls_through
method_name = @method_matcher.expected_method_name
@mock.instance_variable_get(:@receiver)
@original_method = @mock.instance_variable_get(:@mockery).stubba.stubba_methods.find do |mockery|
mockery.method == method_name
end.instance_variable_get(:@original_method)
self
end
def do_call_through(args, &block)
return unless @original_method
@original_method.call(*args, &block)
end
end
end
end
class Mocha::Expectation
include Concerns::Mocha::CallsThrough
end
# Tried to monkey patch this in a modular fashion, but load order seems to be
# screwing it up
class Mocha::Mock
# @private
def method_missing(symbol, *arguments, &block)
if @responder and not @responder.respond_to?(symbol)
raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
end
if matching_expectation_allowing_invocation = all_expectations.match_allowing_invocation(symbol, *arguments)
matching_expectation_allowing_invocation.invoke(&block)
# Hook to enable calls_through
matching_expectation_allowing_invocation.do_call_through(arguments, &block)
else
if (matching_expectation = all_expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
if @unexpected_invocation.nil?
@unexpected_invocation = UnexpectedInvocation.new(self, symbol, *arguments)
matching_expectation.invoke(&block) if matching_expectation
message = @unexpected_invocation.full_description
message << @mockery.mocha_inspect
else
message = @unexpected_invocation.short_description
end
raise ExpectationErrorFactory.build(message, caller)
end
end
end
end
# Usage
Object.expects(:thinger).calls_through
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment