Skip to content

Commit

Permalink
RSpec/SubjectStub. Code review. Remove excessive condition and fix se…
Browse files Browse the repository at this point in the history
…arching of outer example group
  • Loading branch information
andrykonchin committed Jun 9, 2020
1 parent 358d129 commit 956efea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions lib/rubocop/cop/rspec/subject_stub.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/rspec/subject_stub_spec.rb
Expand Up @@ -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 }
Expand Down

0 comments on commit 956efea

Please sign in to comment.