class RuboCop::Cop::Highlands::SpecConstantAssignment

This cop checks for constant assignment inside of specs

@example

# 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

Constants

MESSAGE

Public Instance Methods

on_casgn(node) click to toggle source
# File lib/rubocop/cop/highlands/spec_constant_assignment.rb, line 34
def on_casgn(node)
  return unless in_spec_file?(node)
  parent_module_name = node.parent_module_name
  if node.parent_module_name && parent_module_name != 'Object'
    return
  end
  add_offense(node, message: MESSAGE)
end

Private Instance Methods

in_spec_file?(node) click to toggle source
# File lib/rubocop/cop/highlands/spec_constant_assignment.rb, line 45
def in_spec_file?(node)
  filename = node.location.expression.source_buffer.name

  # For tests, the input is a string
  return true if filename == "(string)"
  filename.include?("/spec/")
end