Class: RuboCop::Cop::RSpec::RemoveConst

Inherits:
Base
  • Object
show all
Extended by:
RSpec::Language::NodePattern
Includes:
RSpec::Language
Defined in:
lib/rubocop/cop/rspec/remove_const.rb

Overview

Checks that ‘remove_const` is not used in specs.

Examples:

# bad
it 'does something' do
  Object.send(:remove_const, :SomeConstant)
end

before do
  SomeClass.send(:remove_const, :SomeConstant)
end

Constant Summary collapse

MSG =
'Do not use remove_const in specs. ' \
'Consider using e.g. `stub_const`.'
RESTRICT_ON_SEND =
%i[send __send__].freeze

Instance Method Summary collapse

Methods included from RSpec::Language::NodePattern

block_or_numblock_pattern, block_pattern, numblock_pattern, send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Methods inherited from Base

inherited, #on_new_investigation

Instance Method Details

#on_send(node) ⇒ Object

Check for offenses



32
33
34
35
36
# File 'lib/rubocop/cop/rspec/remove_const.rb', line 32

def on_send(node)
  remove_const(node) do
    add_offense(node)
  end
end

#remove_const(node) ⇒ Object



27
28
29
# File 'lib/rubocop/cop/rspec/remove_const.rb', line 27

def_node_matcher :remove_const, <<~PATTERN
  (send _ {:send | :__send__} (sym :remove_const) _)
PATTERN