title: What is the best way to write specs for code that depends on environment variables?
body: <p>I am testing some code that pulls its configuration from environment variables (set by Heroku config vars in production, for local development I use foreman).</p>
  
  <p>What's the best way to test this kind of code with RSpec?</p>
  
  <p>I came up with this:</p>
  
  <pre><code>before :each do
      ENV.stub(:[]).with("AWS_ACCESS_KEY_ID").and_return("asdf")
      ENV.stub(:[]).with("AWS_SECRET_ACCESS_KEY").and_return("secret")
  end
  </code></pre>
  
  <p>If you don't need to test different values of the environment variables, I guess you could set them in <code>spec_helper</code> instead.</p>