Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow expectations on subclass any instance methods. #1130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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