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

Commit 8db6ccc

Browse files
committed
Prevent warning when let is overridden in an include
1 parent cc68276 commit 8db6ccc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/rspec/core/memoized_helpers.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,20 @@ 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 this module, (e.g. the `include_super = false`)
294+
# then we need to remove it to prevent a warning
295+
if our_module.instance_methods(false).include?(name)
296+
our_module.__send__(:remove_method, name)
297+
end
298+
our_module.__send__(:define_method, name, &block)
299+
300+
# If we have a module clash in the example module, (e.g. the `include_super = false`)
301+
# then we need to remove it to prevent a warning
302+
if instance_methods(false).include?(name)
303+
remove_method(name)
304+
end
292305

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

spec/rspec/core/shared_context_spec.rb

Lines changed: 13 additions & 0 deletions
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)