lib/rspec/mocks/method_double.rb in rspec-mocks-2.12.0 vs lib/rspec/mocks/method_double.rb in rspec-mocks-2.12.1
- old
+ new
@@ -43,10 +43,19 @@
# @private
def original_method
if @method_stasher.method_is_stashed?
# Example: a singleton method defined on @object
method_handle_for(@object, @method_stasher.stashed_method_name)
+ elsif meth = original_unrecorded_any_instance_method
+ # Example: a method that has been mocked through
+ # klass.any_instance.should_receive(:msg).and_call_original
+ # any_instance.should_receive(:msg) causes the method to be
+ # replaced with a proxy method, and then `and_call_original`
+ # is recorded and played back on the object instance. We need
+ # special handling here to get a handle on the original method
+ # object rather than the proxy method.
+ meth
else
begin
# Example: an instance method defined on @object's class.
@object.class.instance_method(@method_name).bind(@object)
rescue NameError
@@ -71,9 +80,22 @@
# If it's not handled, a `NoMethodError` will be raised, just
# like normally.
Proc.new do |*args, &block|
@object.__send__(:method_missing, @method_name, *args, &block)
end
+ end
+
+ def original_unrecorded_any_instance_method
+ return nil unless any_instance_class_recorder_observing_method?(@object.class)
+ alias_name = @object.class.__recorder.build_alias_method_name(@method_name)
+ @object.method(alias_name)
+ end
+
+ def any_instance_class_recorder_observing_method?(klass)
+ return true if klass.__recorder.already_observing?(@method_name)
+ superklass = klass.superclass
+ return false if superklass.nil?
+ any_instance_class_recorder_observing_method?(superklass)
end
if RUBY_VERSION.to_f > 1.8
# @private
def original_method_from_superclass