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

Prevent warning when let is overridden in an include #2593

Merged
merged 1 commit into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/rspec/core/memoized_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,26 @@ def let(name, &block)
raise(
"#let or #subject called with a reserved name #initialize"
) if :initialize == name
MemoizedHelpers.module_for(self).__send__(:define_method, name, &block)
our_module = MemoizedHelpers.module_for(self)

# If we have a module clash in our helper module
# then we need to remove it to prevent a warning.
#
# Note we do not check ancestor modules (see: `instance_methods(false)`)
# as we can override them.
if our_module.instance_methods(false).include?(name)
our_module.__send__(:remove_method, name)
end
our_module.__send__(:define_method, name, &block)

# If we have a module clash in the example module
# then we need to remove it to prevent a warning.
#
# Note we do not check ancestor modules (see: `instance_methods(false)`)
# as we can override them.
if instance_methods(false).include?(name)
remove_method(name)
end

# Apply the memoization. The method has been defined in an ancestor
# module so we can use `super` here to get the value.
Expand Down
13 changes: 13 additions & 0 deletions spec/rspec/core/shared_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@
expect(group.new.foo).to eq('foo')
end

it 'supports overriding let without warnings' do
shared = Module.new do
extend RSpec::SharedContext
let(:foo) { 'foo' }
end
group = RSpec.describe do
include shared
let(:foo) { 'bar' }
end

expect(group.new.foo).to eq('bar')
end

it "supports let when applied to an individual example via metadata" do
shared = Module.new do
extend RSpec::SharedContext
Expand Down