diff --git a/lib/rubocop/cop/rspec/subject_stub.rb b/lib/rubocop/cop/rspec/subject_stub.rb index 0ebbc2918..3b60cf1b4 100644 --- a/lib/rubocop/cop/rspec/subject_stub.rb +++ b/lib/rubocop/cop/rspec/subject_stub.rb @@ -96,17 +96,19 @@ def processed_example_groups def find_all_explicit_subjects(node) node.each_descendant(:block).each_with_object({}) do |child, h| name = subject(child) - if name - h[child.parent.parent] ||= [] - h[child.parent.parent] << name + next unless name + + outer_example_group = child.each_ancestor.find do |a| + example_group?(a) end + + h[outer_example_group] ||= [] + h[outer_example_group] << name end end def find_subject_expectations(node, subject_names = [], &block) - if example_group?(node) && @explicit_subjects[node] - subject_names = @explicit_subjects[node] - end + subject_names = @explicit_subjects[node] if @explicit_subjects[node] expectation_detected = (subject_names + [:subject]).any? do |name| message_expectation?(node, name) diff --git a/spec/rubocop/cop/rspec/subject_stub_spec.rb b/spec/rubocop/cop/rspec/subject_stub_spec.rb index 728d1fc7d..712fb84fe 100644 --- a/spec/rubocop/cop/rspec/subject_stub_spec.rb +++ b/spec/rubocop/cop/rspec/subject_stub_spec.rb @@ -21,7 +21,7 @@ end it 'flags when subject is stubbed and there are several named subjects ' \ - 'in the same example group', :wip do + 'in the same example group' do expect_offense(<<-RUBY) describe Foo do subject(:foo) { described_class.new }