Module: Greenbar::EnvSetup

Includes:
TestSetup
Defined in:
lib/greenbar/EnvSetup.rb

Overview

:startdoc:

Purpose

Save and restore The Environment (ENV) automatically. If you’ve got a piece of code that depends on environment variables, then you’ll want this to do good unit testing. The variable needs to be set to make sure the test passes, but you want to restore everything to a consistent state when the test ends.

Usage

  • Include Greenbar::EnvSetup in your test case.

  • Define any ENV settings you need.

You may now modify the ENV freely, confident that it will be restored to it’s starting configuration.

Example

Let’s say you’ve got an application, and it needs to know find it’s configuration file. To do this, you want it to look in the directory specified in the environment variable APP_HOME.

:include: doc/examples/EnvSetup.rb

Instance Method Summary collapse

Methods included from TestSetup

included, #setup, #setup_mixins, #teardown, #teardown_mixins

Instance Method Details

#setup_mixinObject

:nodoc:



43
44
45
46
# File 'lib/greenbar/EnvSetup.rb', line 43

def setup_mixin #:nodoc:
  @env_mixin_backup = ENV.to_hash
  ENV.clear
end

#teardown_mixinObject

:nodoc:



48
49
50
# File 'lib/greenbar/EnvSetup.rb', line 48

def teardown_mixin #:nodoc:
  ENV.replace @env_mixin_backup
end