Permalink
Browse files

Simplify extensions to mocha

* Use alias_method_chain to record instead of method patching
* Use more of the public API to require fewer extensions
  • Loading branch information...
1 parent 69c7d82 commit b25a987ccaf5a840c0dd3d12a375b287da42ec6e @jferris jferris committed Feb 24, 2012
Showing with 8 additions and 586 deletions.
  1. +2 −2 lib/bourne/api.rb
  2. +0 −23 lib/bourne/expectation.rb
  3. +5 −28 lib/bourne/mock.rb
  4. +0 −530 test/unit/expectation_test.rb
  5. +1 −3 test/unit/mock_test.rb
View
@@ -41,7 +41,7 @@ def matches?(mock)
@expectations.each do |method, args, block|
@expectation.send(method, *args, &block)
end
- @expectation.invocation_count = invocation_count
+ invocation_count.times { @expectation.invoke }
@expectation.verified?
end
@@ -72,7 +72,7 @@ def matching_invocations
def invocations
Mockery.instance.invocations.select do |invocation|
- invocation.mock.equal?(@mock)
+ invocation.mock.equal?(@mock) || invocation.mock.mocha.equal?(@mock)
end
end
View
@@ -1,23 +0,0 @@
-require 'mocha/expectation'
-
-module Mocha # :nodoc:
- # Extends Mocha::Expectation to record the full arguments and count whenver a
- # stubbed or mocked method is invoked.
- class Expectation # :nodoc:
- attr_accessor :invocation_count
-
- def invoke_with_args(args, &block)
- Mockery.instance.invocation(@mock, method_name, args)
- invoke_without_args(&block)
- end
-
- alias_method :invoke_without_args, :invoke
- alias_method :invoke, :invoke_with_args
-
- private
-
- def method_name
- @method_matcher.expected_method_name
- end
- end
-end
View
@@ -1,36 +1,13 @@
require 'mocha/mock'
-require 'bourne/expectation'
-require 'mocha/expectation_error_factory'
module Mocha # :nodoc:
- # Overwrites #method_missing on Mocha::Mock
- # - pass arguments to .invoke() calls to create Invocation
- # - keep lowest else branch (bourne code)
- # - update from https://github.com/freerange/mocha/blob/master/lib/mocha/mock.rb#L227
- # - update test/unit/mock_test.rb
+ # Extends #method_missing on Mocha::Mock to record Invocations.
class Mock # :nodoc:
+ alias_method :method_missing_without_invocation, :method_missing
+
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 = @expectations.match_allowing_invocation(symbol, *arguments)
- matching_expectation_allowing_invocation.invoke(arguments, &block)
- else
- if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
- matching_expectation.invoke(arguments, &block) if matching_expectation
- message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
- message << @mockery.mocha_inspect
- raise ExpectationErrorFactory.build(message, caller)
- else
- target = if self.respond_to? :mocha
- self.mocha
- else
- mocha
- end
- Mockery.instance.invocation(target, symbol, arguments)
- nil
- end
- end
+ Mockery.instance.invocation(self, symbol, arguments)
+ method_missing_without_invocation(symbol, *arguments, &block)
end
end
end
Oops, something went wrong.

0 comments on commit b25a987

Please sign in to comment.