Ruby-on-rails 如何获得为 rspec 设置的 ENV 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16848553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get an ENV variable set for rspec?
提问by Cyrus
I'm using foreman to start up my rails development server. It's nice that I can put all of my environment variables in the .envfile. Is there a way to do something similar for my test environment?
我正在使用 foreman 来启动我的 Rails 开发服务器。很高兴我可以将所有环境变量放在.env文件中。有没有办法为我的测试环境做类似的事情?
I want to set an API key that I will use with the vcrgem, but I don't want to add the API to version control. Any suggestions besides setting the environment variable manually when I start up my tests script?
我想设置一个将与vcrgem一起使用的 API 密钥,但我不想将 API 添加到版本控制中。除了在启动测试脚本时手动设置环境变量之外,还有什么建议吗?
采纳答案by Jesse Wolgamott
You can use the dotenv gem --- it'll work the same as foreman and load from a .env file. (and a .env.test file for your test environments)
您可以使用 dotenv gem --- 它的工作方式与 foreman 相同,并从 .env 文件加载。(以及用于您的测试环境的 .env.test 文件)
回答by Gary S. Weaver
If you just need to set environment variables, you can either set them from command-line:
如果您只需要设置环境变量,您可以从命令行设置它们:
SOMETHING=123 SOMETHING_ELSE="this is a test" rake spec
Or you could define the following at the top of your Rakefile or spec_helper.rb:
或者您可以在 Rakefile 或 spec_helper.rb 的顶部定义以下内容:
ENV['SOMETHING']=123
ENV['SOMETHING_ELSE']="this is a test"
If they don't always apply, you could use a conditional:
如果它们并不总是适用,您可以使用条件:
if something_needs_to_happen?
ENV['SOMETHING']=123
ENV['SOMETHING_ELSE']="this is a test"
end
If you want to use a Foreman .envfile, which looks like:
如果你想使用 Foreman.env文件,它看起来像:
SOMETHING=123
SOMETHING_ELSE="this is a test"
and turn it into the following and eval it:
并将其转换为以下内容并对其进行评估:
ENV['SOMETHING']='123'
ENV['SOMETHING_ELSE']='this is a test'
You might do:
你可能会这样做:
File.open("/path/to/.env", "r").each_line do |line|
a = line.chomp("\n").split('=',2)
a[1].gsub!(/^"|"$/, '') if ['\'','"'].include?(a[1][0])
eval "ENV['#{a[0]}']='#{a[1] || ''}'"
end
though I don't think that would work for multi-line values.
虽然我认为这不适用于多行值。
And as @JesseWolgamott noted, it looks like you could use gem 'dotenv-rails'.
正如@JesseWolgamott 指出的那样,您似乎可以使用gem 'dotenv-rails'.
回答by AJcodez
One option is to alias the rspec command to be a little more specific. Put the following line in your dotfiles (.bashrcor .profileor something).
一种选择是将 rspec 命令的别名更具体一点。把你的点文件(以下线.bashrc或.profile什么的)。
alias 'rspec'='RACK_ENV=test RAILS_ENV=test bundle exec rspec'
Another option is to put environment variables in specific .env files:
另一种选择是将环境变量放在特定的 .env 文件中:
# .env.test
RAILS_ENV=test
MONGODB_URI=mongodb://localhost/test
# .. etc ..
Using the dotenvgem works or you can bring them in manually
使用dotenvgem 有效,或者您可以手动将它们引入
$ export $(cat .env.test) && rspec

