Module: RSpec::StubbedEnv::StubHelpers — Documentation by YARD 0.9.44

# Module: RSpec::StubbedEnv::StubHelpers

Included in:
  HideHelpers

Defined in:
  lib/rspec/stubbed_env/stub_helpers.rb

## Overview

Helpers to unobtrusively stub ENV

## Instance Method Summary collapse

- #stub_env(key_or_hash, value = nil) ⇒ Object

  Can be called with all key value pairs to be stubbed as a hash:.

- #stub_env_hash_accessors ⇒ Object

  Opt in to stubbing ENV hash accessors.

## Instance Method Details

### #stub_env(key_or_hash, value = nil) ⇒ `Object`

Can be called with all key value pairs to be stubbed as a hash:

    stub_env('A' => 'B', 'C' => 'D', 'E' => 'F') # Preferred

Alternatively can be called one per ENV key-value pair to stub:

    stub_env('A', 'B') # NOT
    stub_env('C', 'D') # AS
    stub_env('E', 'F') # GOOD (Creates redundant stubs on values_at)

    40
    41
    42
    43
    44
    45
    46
    47
    # File 'lib/rspec/stubbed_env/stub_helpers.rb', line 40

    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

### #stub_env_hash_accessors ⇒ `Object`

Opt in to stubbing ENV hash accessors. By default, stub_env only stubs
ENV[], ENV.fetch, and ENV.values_at so partial ENV stubs stay narrowly
scoped.

    52
    53
    54
    55
    56
    # File 'lib/rspec/stubbed_env/stub_helpers.rb', line 52

    def stub_env_hash_accessors
      init_stub unless env_stubbed?
      allow(ENV).to(receive(:to_hash) { mixed_env_hash })
      allow(ENV).to(receive(:to_h) { mixed_env_hash })
    end