diff options
author | Brett Walker <brett@digitalmoksha.com> | 2018-01-23 13:23:23 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzegorz@gitlab.com> | 2018-01-23 13:23:23 +0300 |
commit | b8b9e9eb8e3f285bc3040f3dc425deeada585aae (patch) | |
tree | 87934fbe6b2b2b66be25888152bcbcd4af82e970 /qa/spec/support | |
parent | fcfda3ef82992e5f56573dffb56b2a5896c24aff (diff) |
Add ability to drive the API in QA specs
Diffstat (limited to 'qa/spec/support')
-rw-r--r-- | qa/spec/support/stub_env.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/qa/spec/support/stub_env.rb b/qa/spec/support/stub_env.rb new file mode 100644 index 00000000000..bc8f3a5e22e --- /dev/null +++ b/qa/spec/support/stub_env.rb @@ -0,0 +1,38 @@ +# Inspired by https://github.com/ljkbennett/stub_env/blob/master/lib/stub_env/helpers.rb +module Support + module StubENV + def stub_env(key_or_hash, value = nil) + init_stub unless env_stubbed? + + if key_or_hash.is_a? Hash + key_or_hash.each { |k, v| add_stubbed_value(k, v) } + else + add_stubbed_value key_or_hash, value + end + end + + private + + STUBBED_KEY = '__STUBBED__'.freeze + + def add_stubbed_value(key, value) + allow(ENV).to receive(:[]).with(key).and_return(value) + allow(ENV).to receive(:key?).with(key).and_return(true) + allow(ENV).to receive(:fetch).with(key).and_return(value) + allow(ENV).to receive(:fetch).with(key, anything()) do |_, default_val| + value || default_val + end + end + + def env_stubbed? + ENV[STUBBED_KEY] + end + + def init_stub + allow(ENV).to receive(:[]).and_call_original + allow(ENV).to receive(:key?).and_call_original + allow(ENV).to receive(:fetch).and_call_original + add_stubbed_value(STUBBED_KEY, true) + end + end +end |