Skip to content

Commit 956efea

Browse files
committed
RSpec/SubjectStub. Code review. Remove excessive condition and fix searching of outer example group
1 parent 358d129 commit 956efea

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

lib/rubocop/cop/rspec/subject_stub.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,19 @@ def processed_example_groups
9696
def find_all_explicit_subjects(node)
9797
node.each_descendant(:block).each_with_object({}) do |child, h|
9898
name = subject(child)
99-
if name
100-
h[child.parent.parent] ||= []
101-
h[child.parent.parent] << name
99+
next unless name
100+
101+
outer_example_group = child.each_ancestor.find do |a|
102+
example_group?(a)
102103
end
104+
105+
h[outer_example_group] ||= []
106+
h[outer_example_group] << name
103107
end
104108
end
105109

106110
def find_subject_expectations(node, subject_names = [], &block)
107-
if example_group?(node) && @explicit_subjects[node]
108-
subject_names = @explicit_subjects[node]
109-
end
111+
subject_names = @explicit_subjects[node] if @explicit_subjects[node]
110112

111113
expectation_detected = (subject_names + [:subject]).any? do |name|
112114
message_expectation?(node, name)

spec/rubocop/cop/rspec/subject_stub_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222

2323
it 'flags when subject is stubbed and there are several named subjects ' \
24-
'in the same example group', :wip do
24+
'in the same example group' do
2525
expect_offense(<<-RUBY)
2626
describe Foo do
2727
subject(:foo) { described_class.new }

0 commit comments

Comments
 (0)