Class: RuboCop::Cop::RSpec::LeakyConstantDeclaration
- Defined in:
- lib/rubocop/cop/rspec/leaky_constant_declaration.rb
Overview
Checks that no class, module, or constant is declared.
Constants, including classes and modules, when declared in a block scope, are defined in global namespace, and leak between examples.
If several examples may define a ‘DummyClass`, instead of being a blank slate class as it will be in the first example, subsequent examples will be reopening it and modifying its behavior in unpredictable ways. Even worse when a class that exists in the codebase is reopened.
Anonymous classes are fine, since they don’t result in global namespace name clashes.
Constant Summary collapse
- MSG_CONST =
'Stub constant instead of declaring explicitly.'
- MSG_CLASS =
'Stub class constant instead of declaring explicitly.'
- MSG_MODULE =
'Stub module constant instead of declaring explicitly.'
Instance Method Summary collapse
Methods inherited from Base
inherited, #on_new_investigation
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#on_casgn(node) ⇒ Object
101 102 103 104 105 |
# File 'lib/rubocop/cop/rspec/leaky_constant_declaration.rb', line 101 def on_casgn(node) return unless inside_describe_block?(node) add_offense(node, message: MSG_CONST) end |
#on_class(node) ⇒ Object
107 108 109 110 111 |
# File 'lib/rubocop/cop/rspec/leaky_constant_declaration.rb', line 107 def on_class(node) return unless inside_describe_block?(node) add_offense(node, message: MSG_CLASS) end |
#on_module(node) ⇒ Object
113 114 115 116 117 |
# File 'lib/rubocop/cop/rspec/leaky_constant_declaration.rb', line 113 def on_module(node) return unless inside_describe_block?(node) add_offense(node, message: MSG_MODULE) end |