From 5b5020a3bcd1e0d05f0c0e6c98393ed0a1a84421 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Fri, 23 Oct 2020 03:53:16 +0800 Subject: [PATCH] Fix the case for extend self + any instance of --- lib/rspec/mocks/instance_method_stasher.rb | 5 ++++- spec/rspec/mocks/stub_spec.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/rspec/mocks/instance_method_stasher.rb b/lib/rspec/mocks/instance_method_stasher.rb index f83b3930a..a318a8b6b 100644 --- a/lib/rspec/mocks/instance_method_stasher.rb +++ b/lib/rspec/mocks/instance_method_stasher.rb @@ -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 diff --git a/spec/rspec/mocks/stub_spec.rb b/spec/rspec/mocks/stub_spec.rb index 6f9f14718..dd5852405 100644 --- a/spec/rspec/mocks/stub_spec.rb +++ b/spec/rspec/mocks/stub_spec.rb @@ -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