This repository was archived by the owner on Nov 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +31
-7
lines changed Expand file tree Collapse file tree 3 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -139,7 +139,9 @@ def method_owned_by_klass?
139
139
# The owner of M.b is the raw Module object, instead of the expected
140
140
# singleton class of the module
141
141
return true if RUBY_VERSION < '1.9' && owner == @object
142
- owner == @klass || !( method_defined_on_klass? ( owner ) )
142
+ owner == @klass ||
143
+ owner . singleton_class == @klass || # When `extend self` is used
144
+ !( method_defined_on_klass? ( owner ) )
143
145
end
144
146
end
145
147
end
Original file line number Diff line number Diff line change @@ -83,8 +83,11 @@ def restore_original_method
83
83
return unless @method_is_proxied
84
84
85
85
remove_method_from_definition_target
86
- @method_stasher . restore if @method_stasher . method_is_stashed?
87
- restore_original_visibility
86
+
87
+ if @method_stasher . method_is_stashed?
88
+ @method_stasher . restore
89
+ restore_original_visibility
90
+ end
88
91
89
92
@method_is_proxied = false
90
93
end
@@ -102,10 +105,7 @@ def show_frozen_warning
102
105
103
106
# @private
104
107
def restore_original_visibility
105
- return unless @original_visibility &&
106
- MethodReference . method_defined_at_any_visibility? ( object_singleton_class , @method_name )
107
-
108
- object_singleton_class . __send__ ( @original_visibility , method_name )
108
+ method_owner . __send__ ( @original_visibility , @method_name )
109
109
end
110
110
111
111
# @private
@@ -261,6 +261,15 @@ def definition_target
261
261
262
262
private
263
263
264
+ def method_owner
265
+ @method_owner ||=
266
+ if object . kind_of? ( Object )
267
+ Object . instance_method ( :method ) . bind ( object ) . call ( @method_name ) . owner
268
+ else
269
+ object . method ( @method_name ) . owner
270
+ end
271
+ end
272
+
264
273
def remove_method_from_definition_target
265
274
definition_target . __send__ ( :remove_method , @method_name )
266
275
rescue NameError
Original file line number Diff line number Diff line change @@ -138,6 +138,10 @@ module ToBePrepended
138
138
def value
139
139
"#{ super } _prepended" . to_sym
140
140
end
141
+
142
+ def value_without_super
143
+ :prepended
144
+ end
141
145
end
142
146
143
147
it "handles stubbing prepended methods" do
@@ -165,6 +169,15 @@ def object.value; :original; end
165
169
expect ( object . value ) . to eq :stubbed
166
170
end
167
171
172
+ it "handles stubbing prepending methods that were only defined on the prepended module" do
173
+ object = Object . new
174
+ object . singleton_class . send ( :prepend , ToBePrepended )
175
+
176
+ expect ( object . value_without_super ) . to eq :prepended
177
+ allow ( object ) . to receive ( :value_without_super ) { :stubbed }
178
+ expect ( object . value_without_super ) . to eq :stubbed
179
+ end
180
+
168
181
it 'does not unnecessarily prepend a module when the prepended module does not override the stubbed method' do
169
182
object = Object . new
170
183
def object . value ; :original ; end
You can’t perform that action at this time.
0 commit comments