Class: GLRubocop::GLCops::NoStubbingEnv
- Inherits:
-
RuboCop::Cop::Base
- Object
- RuboCop::Cop::Base
- GLRubocop::GLCops::NoStubbingEnv
- Defined in:
- lib/gl_rubocop/gl_cops/no_stubbing_env.rb
Overview
This cop ensures that you don’t stub ENV with ‘allow`/`expect` + `receive`.
Stubbing ENV this way (even with ‘and_call_original`) only stubs the env vars the spec explicitly knows about. Any var that is read but not stubbed - for example a var that is present on CI but not locally, or vice versa - behaves inconsistently and causes flaky failures.
Instead, replace ENV wholesale with ‘stub_const`, building the hash from the real ENV so unrelated vars keep working everywhere:
Good:
stub_const('ENV', ENV.to_hash.except('VAR_TO_UNSET').merge('VAR_TO_SET' => 'value'))
Bad:
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('VAR_TO_SET').and_return('value')
expect(ENV).to receive(:fetch)
Constant Summary collapse
- MSG =
"Don't stub ENV with allow/expect + receive. Use " \ "stub_const('ENV', ENV.to_hash.except('VAR_TO_UNSET')." \ "merge('VAR_TO_SET' => 'value')) instead, so unrelated ENV vars " \ '(e.g. on CI) keep working.'.freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
35 36 37 38 39 |
# File 'lib/gl_rubocop/gl_cops/no_stubbing_env.rb', line 35 def on_send(node) return unless stubbing_env?(node) add_offense(node) end |