Easily manage your environment.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install climate_control
Climate Control can be used to temporarily assign environment variables within a block:
ClimateControl.modify CONFIRMATION_INSTRUCTIONS_BCC: 'email@example.com' do sign_up_as 'firstname.lastname@example.org' confirm_account_for_email 'email@example.com' expect(current_email).to bcc_to('firstname.lastname@example.org') end
To modify multiple environment variables:
ClimateControl.modify CONFIRMATION_INSTRUCTIONS_BCC: 'email@example.com', MAIL_FROM: 'firstname.lastname@example.org' do sign_up_as 'email@example.com' confirm_account_for_email 'firstname.lastname@example.org' expect(current_email).to bcc_to('email@example.com') expect(current_email).to be_from('firstname.lastname@example.org') end
To use with RSpec, you could define this in your spec:
def with_modified_env(options, &block) ClimateControl.modify(options, &block) end
This would allow for more straightforward way to modify the environment:
require 'spec_helper' describe Thing, 'name' do it 'appends ADDITIONAL_NAME' do with_modified_env ADDITIONAL_NAME: 'bar' do expect(Thing.new.name).to eq('John Doe Bar') end end def with_modified_env(options, &block) ClimateControl.modify(options, &block) end end
To modify the environment for an entire set of tests in RSpec, use an
describe Thing, 'name' do # ... tests around do |example| ClimateControl.modify FOO: 'bar' do example.run end end end
Environment variables assigned within the block will be preserved;
essentially, the code should behave exactly the same with and without the
block, except for the overrides. Transparency is crucial because the code
executed within the block is not for
ClimateControl to manage or modify. See
the tests for more detail about the specific behaviors.
Why Use Climate Control?
By following guidelines regarding environment variables outlined by the twelve-factor app, testing code in an isolated manner becomes more difficult:
- avoiding modifications and testing values, we introduce mystery guests
- making modifications and testing values, we introduce risk as environment variables represent global state
Climate Control modifies environment variables only within the context of the block, ensuring values are managed properly and consistently.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request
This project uses StandardRB to ensure formatting.
climate_control is copyright 2012-2021 Joshua Clayton and thoughtbot, inc. It is free software and may be redistributed under the terms specified in the LICENSE file.