Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for #and_call_original to an #any_instance partial mock.

Fixes #197.
  • Loading branch information...
commit 0f77e46c359b92b98781492ca95b6b4ae684ded1 1 parent 0d6eea7
@myronmarston myronmarston authored
View
9 Changelog.md
@@ -1,3 +1,12 @@
+### dev
+[full
+changelog](http://github.com/rspec/rspec-mocks/compare/v2.12.0...master)
+
+Enhancements
+
+* Add support for `and_call_original` to an `any_instance` partial mock.
+ (Myron Marston)
+
### 2.12.0 / 2012-11-12
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.11.3...v2.12.0)
View
1  lib/rspec/mocks/any_instance/chain.rb
@@ -25,6 +25,7 @@ def #{method_name}(*args, &block)
record :and_raise
record :and_throw
record :and_yield
+ record :and_call_original
record :with
record :once
record :twice
View
8 lib/rspec/mocks/any_instance/recorder.rb
@@ -109,6 +109,10 @@ def instance_that_received(method_name)
@played_methods[method_name]
end
+ def build_alias_method_name(method_name)
+ "__#{method_name}_without_any_instance__"
+ end
+
private
def normalize_chain(*args)
@@ -130,10 +134,6 @@ def restore_method!(method_name)
end
end
- def build_alias_method_name(method_name)
- "__#{method_name}_without_any_instance__"
- end
-
def restore_original_method!(method_name)
alias_method_name = build_alias_method_name(method_name)
@klass.class_eval do
View
8 lib/rspec/mocks/method_double.rb
@@ -45,6 +45,8 @@ 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
+ meth
else
begin
# Example: an instance method defined on @object's class.
@@ -75,6 +77,12 @@ def original_method
end
end
+ def original_unrecorded_any_instance_method
+ alias_name = @object.class.__recorder.build_alias_method_name(@method_name)
+ return nil unless @object.respond_to?(alias_name)
+ @object.method(alias_name)
+ end
+
if RUBY_VERSION.to_f > 1.8
# @private
def original_method_from_superclass
View
5 spec/rspec/mocks/and_call_original_spec.rb
@@ -37,6 +37,11 @@ def instance.foo; :bar; end
expect(instance.foo).to eq(:bar)
end
+ it 'works for an any_instance partial mock' do
+ klass.any_instance.should_receive(:meth_1).and_call_original
+ expect(klass.new.meth_1).to eq(:original)
+ end
+
if RUBY_VERSION.to_f > 1.8
it 'works for class methods defined on a superclass' do
subclass = Class.new(klass)
Please sign in to comment.
Something went wrong with that request. Please try again.