Skip to content

Commit 341ae44

Browse files
committed
RSpec/SubjectStub. Code review. Use #on_top_level_describe instead of #on_block callback
1 parent 7e76bd7 commit 341ae44

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

lib/rubocop/cop/rspec/subject_stub.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module RSpec
2222
# end
2323
#
2424
class SubjectStub < Cop
25+
include RuboCop::RSpec::TopLevelDescribe
26+
2527
MSG = 'Do not stub methods of the object under test.'
2628

2729
# @!method subject(node)
@@ -75,24 +77,16 @@ class SubjectStub < Cop
7577
} ...)
7678
PATTERN
7779

78-
def on_block(node)
79-
return unless example_group?(node)
80-
return unless (processed_example_groups & node.ancestors).empty?
81-
82-
processed_example_groups << node
83-
@explicit_subjects = find_all_explicit_subjects(node)
80+
def on_top_level_describe(node, _args)
81+
@explicit_subjects = find_all_explicit_subjects(node.parent)
8482

85-
find_subject_expectations(node) do |stub|
83+
find_subject_expectations(node.parent) do |stub|
8684
add_offense(stub)
8785
end
8886
end
8987

9088
private
9189

92-
def processed_example_groups
93-
@processed_example_groups ||= Set.new
94-
end
95-
9690
def find_all_explicit_subjects(node)
9791
node.each_descendant(:block).each_with_object({}) do |child, h|
9892
name = subject(child)

0 commit comments

Comments
 (0)