使用Foreman时如何为测试和开发设置不同的环境变量
How to set different environment variables for test and development when using Foreman
目前,我将敏感信息(例如 API 密钥)存储在由工头 gem 加载的 .env
文件中。这很好用,我能够访问在开发和测试中相同的环境变量。
我希望能够为我的测试环境使用不同于我的开发环境的一组 API 密钥。
我尝试根据我所看到的 https://www.rubydoc.info/gems/dotenv/2.7.5
做类似 .env.development
和 .env.test
的事情
万一它有用,这里是我正在使用的通用堆栈:Rails 5.2.3、Minitest、Guard(运行 我的保存测试)和 Foreman。目前此应用程序正在部署到 Heroku。
这是我的 test_helper.rb
:
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'vcr'
VCR.configure do |config|
config.cassette_library_dir = "test/cassettes"
config.hook_into :faraday
end
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
我目前的解决方法是增加一个后缀为 _test
的环境变量,我用它来覆盖 test_helper.rb
中的 ENV。不过我觉得不对。
默认情况下将使用 .env
文件,但您可以创建第二个文件,例如 .env2
并将其与 foreman start --env .env2
一起使用。
如果您正在使用 dotenv-rails gem 加载环境变量,.env.test.local
将仅针对测试环境自动加载。
目前,我将敏感信息(例如 API 密钥)存储在由工头 gem 加载的 .env
文件中。这很好用,我能够访问在开发和测试中相同的环境变量。
我希望能够为我的测试环境使用不同于我的开发环境的一组 API 密钥。
我尝试根据我所看到的 https://www.rubydoc.info/gems/dotenv/2.7.5
做类似.env.development
和 .env.test
的事情
万一它有用,这里是我正在使用的通用堆栈:Rails 5.2.3、Minitest、Guard(运行 我的保存测试)和 Foreman。目前此应用程序正在部署到 Heroku。
这是我的 test_helper.rb
:
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'vcr'
VCR.configure do |config|
config.cassette_library_dir = "test/cassettes"
config.hook_into :faraday
end
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
我目前的解决方法是增加一个后缀为 _test
的环境变量,我用它来覆盖 test_helper.rb
中的 ENV。不过我觉得不对。
默认情况下将使用 .env
文件,但您可以创建第二个文件,例如 .env2
并将其与 foreman start --env .env2
一起使用。
如果您正在使用 dotenv-rails gem 加载环境变量,.env.test.local
将仅针对测试环境自动加载。