Skip to content

Commit

Permalink
Prevent warning when let is overridden in an include (#2593)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Feb 1, 2019
1 parent d598ecb commit e4e10d9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/rspec/core/memoized_helpers.rb
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
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

0 comments on commit e4e10d9

Please sign in to comment.