From 341ae440ff17e2bc1dadf3c025c46698152f0cd2 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Tue, 9 Jun 2020 00:15:28 +0300 Subject: [PATCH] RSpec/SubjectStub. Code review. Use #on_top_level_describe instead of #on_block callback --- lib/rubocop/cop/rspec/subject_stub.rb | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/rubocop/cop/rspec/subject_stub.rb b/lib/rubocop/cop/rspec/subject_stub.rb index 0ebbc2918..1c57d2cca 100644 --- a/lib/rubocop/cop/rspec/subject_stub.rb +++ b/lib/rubocop/cop/rspec/subject_stub.rb @@ -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) @@ -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)