From a255002d6ba86360c243a3c1828fb01919fab5c6 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Thu, 8 Dec 2016 21:09:04 +1100 Subject: [PATCH] use klass name to stash methods in case of subclassing --- lib/rspec/mocks/any_instance/recorder.rb | 2 +- spec/rspec/mocks/any_instance_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/rspec/mocks/any_instance/recorder.rb b/lib/rspec/mocks/any_instance/recorder.rb index 0db97af31..05ac954e8 100644 --- a/lib/rspec/mocks/any_instance/recorder.rb +++ b/lib/rspec/mocks/any_instance/recorder.rb @@ -115,7 +115,7 @@ def instance_that_received(method_name) # @private def build_alias_method_name(method_name) - "__#{method_name}_without_any_instance__" + "__#{@klass}__#{method_name}_without_any_instance__" end # @private diff --git a/spec/rspec/mocks/any_instance_spec.rb b/spec/rspec/mocks/any_instance_spec.rb index 656f7d904..fe4cfed6d 100644 --- a/spec/rspec/mocks/any_instance_spec.rb +++ b/spec/rspec/mocks/any_instance_spec.rb @@ -168,6 +168,24 @@ def private_method; :private_method_return_value; end end end + it "allows an expectation to be set on a subclass when an allowance already exists on a superclass" do + a = Class.new do + def foo + true + end + end + + b = Class.new(a) do + def foo + super + end + end + + allow_any_instance_of(a).to receive(:foo) + expect_any_instance_of(b).to receive(:foo).with(:bar) + b.new.foo(:bar) + end + context "when the class has a prepended module", :if => Support::RubyFeatures.module_prepends_supported? do it 'allows stubbing a method that is not defined on the prepended module' do klass.class_eval { prepend Module.new { def other; end } }