Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autorestore ENV between tests #472

Merged
merged 9 commits into from
Jan 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update README
bkeepers committed Jan 24, 2024
commit 234e126d0fd16a523468465aca91f8d0fdc276a6
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -52,6 +52,14 @@ require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
```

## Autorestore in tests

Since 3.0, dotenv in a Rails app will automatically restore `ENV` to its original state before each test. This means you can modify `ENV` in your tests without fear of leaking state to other tests. It works with both `ActiveSupport::TestCase` and `Rspec`.

To disable this behavior, set `config.dotenv.autorestore = false` in `config/application.rb` or `config/environments/test.rb`.

To use this behavior outside of a Rails app, just `require "dotenv/autorestore"` in your test suite.

### Rake

To ensure `.env` is loaded in rake, load the tasks: