I am working on setting up rubocop on a test suite I've taken over. The test suite contains a lib/constants.rb config that is made available to the test suite, and all tests are all found in a spec/* folder. I am setting up rubocop so that it only runs against the test specs (spec/*), and ignores the lib folder.
I have set up RSpec/LeakyConstantDeclaration with rubocop to ensure nobody is potentially re-assigning any of the global constants. This cop rule works for any specs written like this, and rubocop will correctly flag RSpec/LeakyConstantDeclaration on this file:
# spec/some_test_spec.rb
describe 'some test' do
SOME_CONSTANT = { 'blah' => 'blah' }
# SOME_CONSTANT used somewhere in here
it 'tests something' do
...
end
end
However, I've recently found some more tests that are also doing this (EEK!):
# spec/some_other_test_spec.rb
SOME_CONSTANT = { 'blah' => 'blah' }
describe 'some other test' do
# SOME_CONSTANT used somewhere in here
it 'tests something else' do
...
end
end
Some devs have started declaring their own constants explicitly in the global scope, and this could also potentially re-assign a constant created in the lib/constants.rb.
Since I'm running rubocop only against the test folder of spec/* is there any way to write a rubocop convention rule that disallows constants, and recommends using let or a local variable instead?
I'm thinking I may have to write a custom Cop to do this, but wondering if there is a known existing way to do it?
Thanks.