Skip to content

Commit

Permalink
use klass name to stash methods in case of subclassing
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Sep 9, 2019
1 parent 2aab10d commit a255002
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rspec/mocks/any_instance/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/mocks/any_instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand Down

0 comments on commit a255002

Please sign in to comment.