I am using gem 'dotenv-rails', '~> 0.9.0'
to load environment variables into a Rails 4.0.5 app. I have a .env file along with a .env.test. All is working well in development, however when it comes to testing, which I do with rspec, it is failing to set the environment variables.
I have gone into Rails console in testing environment and I can see they are set to nil.
Any tips on how to get dotenv to load in testing?
This is a late answer, but for anyone who may find it helpful, add this to spec_helper.rb
require 'dotenv'
Dotenv.load('.env')
Also, Dotenv.load can accept a list, for example:
Dotenv.load(
'.env.local',
'.env.test',
'.env'
)
将此行放在 rspec 设置中的Dotenv::Railtie.load
或要加载 dot env 文件的任何页面中。
Dotenv.load(File.expand_path("../../.env.#{Rails.env}", __FILE__))
在研究 Paritosh 的回答后,我发现需要为每个环境指定文件。
If you have test specific environemnt config, You can use Dotenv.overload
to overwrite other env config. Just add the following to your spec_helper.rb
or rails_helper.rb
.
require "dotenv"
Dotenv.overload ".env.test"
Dotenv.overload ".env.test.local"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.