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

Commit e4e10d9

Browse files
authored
Prevent warning when let is overridden in an include (#2593)
1 parent d598ecb commit e4e10d9

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/rspec/core/memoized_helpers.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,26 @@ def let(name, &block)
288288
raise(
289289
"#let or #subject called with a reserved name #initialize"
290290
) if :initialize == name
291-
MemoizedHelpers.module_for(self).__send__(:define_method, name, &block)
291+
our_module = MemoizedHelpers.module_for(self)
292+
293+
# If we have a module clash in our helper module
294+
# then we need to remove it to prevent a warning.
295+
#
296+
# Note we do not check ancestor modules (see: `instance_methods(false)`)
297+
# as we can override them.
298+
if our_module.instance_methods(false).include?(name)
299+
our_module.__send__(:remove_method, name)
300+
end
301+
our_module.__send__(:define_method, name, &block)
302+
303+
# If we have a module clash in the example module
304+
# then we need to remove it to prevent a warning.
305+
#
306+
# Note we do not check ancestor modules (see: `instance_methods(false)`)
307+
# as we can override them.
308+
if instance_methods(false).include?(name)
309+
remove_method(name)
310+
end
292311

293312
# Apply the memoization. The method has been defined in an ancestor
294313
# module so we can use `super` here to get the value.

spec/rspec/core/shared_context_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@
6666
expect(group.new.foo).to eq('foo')
6767
end
6868

69+
it 'supports overriding let without warnings' do
70+
shared = Module.new do
71+
extend RSpec::SharedContext
72+
let(:foo) { 'foo' }
73+
end
74+
group = RSpec.describe do
75+
include shared
76+
let(:foo) { 'bar' }
77+
end
78+
79+
expect(group.new.foo).to eq('bar')
80+
end
81+
6982
it "supports let when applied to an individual example via metadata" do
7083
shared = Module.new do
7184
extend RSpec::SharedContext

0 commit comments

Comments
 (0)