How to change test environment variables

There are times when you might want to change your environment variables within a testing environment. Maybe you want to communicate with a different server or want to enable some kind of functionality that is normally disabled for testing. You can use stubbing to elegantly and temporarily just set your environment variables within a test.

ENV.stub(:[]).with('FEATURE_ENABLED').and_return('true')

Here, I am enabling a feature that allows us to access user information. Normally, the lookup is computationally expensive so we don’t run it on all of our tests. But when the need arises, it’s good to be able to change your environment variables easily within a single test.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s