Skip to content

Commit

Permalink
Fix the case for extend self + any instance of
Browse files Browse the repository at this point in the history
  • Loading branch information
godfat committed Oct 22, 2020
1 parent 29359ee commit 5b5020a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/rspec/mocks/instance_method_stasher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ def method_owned_by_klass?
# The owner of M.b is the raw Module object, instead of the expected
# singleton class of the module
return true if RUBY_VERSION < '1.9' && owner == @object

owner == @klass ||
owner.singleton_class == @klass || # When `extend self` is used
# When `extend self` is used, and not under any instance of
(owner.singleton_class == @klass &&
!Mocks.space.any_instance_recorder_for(owner, true)) ||
!(method_defined_on_klass?(owner))
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/rspec/mocks/stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,21 @@ class << self; public :hello; end;
expect(mod.hello).to eq(:hello)
end

it "correctly restores from allow_any_instance_of for self extend" do
mod = Module.new {
extend self
def hello; :hello; end
}

allow_any_instance_of(mod).to receive(:hello) { :stub }

expect(mod.hello).to eq(:stub)

reset_all

expect(mod.hello).to eq(:hello)
end

it "correctly handles stubbing inherited mixed in class methods" do
mod = Module.new do
def method_a
Expand Down

0 comments on commit 5b5020a

Please sign in to comment.