Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit a063443

Browse files
committed
Fix the case for extend self + any instance of
1 parent b50456c commit a063443

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/rspec/mocks/instance_method_stasher.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ def method_owned_by_klass?
139139
# The owner of M.b is the raw Module object, instead of the expected
140140
# singleton class of the module
141141
return true if RUBY_VERSION < '1.9' && owner == @object
142+
142143
owner == @klass ||
143-
owner.singleton_class == @klass || # When `extend self` is used
144+
# When `extend self` is used, and not under any instance of
145+
(owner.singleton_class == @klass &&
146+
!Mocks.space.any_instance_recorder_for(owner, true)) ||
144147
!(method_defined_on_klass?(owner))
145148
end
146149
end

spec/rspec/mocks/stub_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,21 @@ class << self; public :hello; end;
363363
expect(mod.hello).to eq(:hello)
364364
end
365365

366+
it "correctly restores from allow_any_instance_of for self extend" do
367+
mod = Module.new {
368+
extend self
369+
def hello; :hello; end
370+
}
371+
372+
allow_any_instance_of(mod).to receive(:hello) { :stub }
373+
374+
expect(mod.hello).to eq(:stub)
375+
376+
reset_all
377+
378+
expect(mod.hello).to eq(:hello)
379+
end
380+
366381
it "correctly handles stubbing inherited mixed in class methods" do
367382
mod = Module.new do
368383
def method_a

0 commit comments

Comments
 (0)