Class: RuboCop::Cop::Airbnb::SpecConstantAssignment

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/airbnb/spec_constant_assignment.rb

Overview

This cop checks for constant assignment inside of specs

Examples:

# bad
describe Something do
  PAYLOAD = [1, 2, 3]
end

# good
describe Something do
  let(:payload)  { [1, 2, 3] }
end

# bad
describe Something do
  MyClass::PAYLOAD = [1, 2, 3]
end

# good
describe Something do
  before { stub_const('MyClass::PAYLOAD', [1, 2, 3])
end

Constant Summary collapse

MESSAGE =
"Defining constants inside of specs can cause spurious behavior. " \
"It is almost always preferable to use `let` statements, " \
"anonymous class/module definitions, or stub_const".freeze

Instance Method Summary collapse

Instance Method Details

#on_casgn(node) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/airbnb/spec_constant_assignment.rb', line 33

def on_casgn(node)
  return unless in_spec_file?(node)
  parent_module_name = node.parent_module_name
  if parent_module_name && parent_module_name != "Object"
    return
  end
  add_offense(node, message: MESSAGE)
end