Skip to content

Commit

Permalink
RSpec/SubjectStub. Code review. Use #on_top_level_describe instead of…
Browse files Browse the repository at this point in the history
… #on_block callback
  • Loading branch information
andrykonchin committed Jun 8, 2020
1 parent 7e76bd7 commit 592f992
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions lib/rubocop/cop/rspec/subject_stub.rb
Expand Up @@ -22,6 +22,8 @@ module RSpec
# end
#
class SubjectStub < Cop
include RuboCop::RSpec::TopLevelDescribe

MSG = 'Do not stub methods of the object under test.'

# @!method subject(node)
Expand Down Expand Up @@ -75,24 +77,16 @@ class SubjectStub < Cop
} ...)
PATTERN

def on_block(node)
return unless example_group?(node)
return unless (processed_example_groups & node.ancestors).empty?

processed_example_groups << node
@explicit_subjects = find_all_explicit_subjects(node)
def on_top_level_describe(node, args)
@explicit_subjects = find_all_explicit_subjects(node.parent)

find_subject_expectations(node) do |stub|
find_subject_expectations(node.parent) do |stub|
add_offense(stub)
end
end

private

def processed_example_groups
@processed_example_groups ||= Set.new
end

def find_all_explicit_subjects(node)
node.each_descendant(:block).each_with_object({}) do |child, h|
name = subject(child)
Expand Down

0 comments on commit 592f992

Please sign in to comment.