ENV stubbing via a shared context for more powerful tests.

Total Downloads Downloads Today Build Status Maintainability Test Coverage Network Stars Version Build Documentation Depfu Open Source Helpers Chat License: MIT

Installation

Add this line to your application’s Gemfile:

gem 'rspec-stubbed_env', group: :test

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-stubbed_env

You must configure RSpec to use the :expect syntax, or some compatible alternative.

RSpec.configure do |config|
  config.expect_with :rspec do |c|
    c.syntax = :expect
  end
end

Require the library in your spec/test helper somewhere:

require 'rspec/stubbed_env'

Usage

ENV stubbing:

  • is opt-in, via a shared context, rather than global.
  • does not affect the real ENV at all. It is a true stub.
  • has the same scope as a before, subject, or let at the same level.

See the spec suite for detailed examples.

# This is normal, without stubbing, ENV is not set
describe 'vanilla' do
  it 'has no ENV stub' do
    expect(ENV['FOO']).to be_nil
  end
end

# With a stubbed ENV!
describe 'my stubbed test' do
  include_context 'with stubbed env'
  before do
    stub_env('FOO' => 'is bar')
  end
  it 'has a value' do
    expect(ENV['FOO']).to eq('is bar')
  end
end