如何替换 Rspec 的 ENV[“RAILS_ENV”] ||= 'test' Rails 3.0 中已弃用?
我正在使用 Rails (3.0.4) 和 rspec-rails (2.5.0)。当我运行 rails generated rspec:install它生成包含此行的
spec_helper.rb`:
ENV["RAILS_ENV"] ||= 'test'
当我运行 rake spec
时,我在终端中收到此警告:
DEPRECATION WARNING: RAILS_ENV is deprecated. Please use ::Rails.env.
这并不那么烦人,因为它只在我的 < 中运行一次code>Spork.prefork,但如果可能的话,我想克服这种弃用。我是 Rails 新手,在 rspec-rails 问题 或其他任何地方。
I'm using rails (3.0.4)
and rspec-rails (2.5.0). When I run
rails generate rspec:install
spec_helper.rb` that contains this line:
it produces
ENV["RAILS_ENV"] ||= 'test'
When I run rake spec
I get this warning on in the terminal:
DEPRECATION WARNING: RAILS_ENV is deprecated. Please use ::Rails.env.
This isn't as annoying since that only runs once inside my Spork.prefork
, but I'd like to get past that deprecation if possible. I'm new to Rails and haven't found mention of this in the rspec-rails issues or anywhere else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 GitHub 问题中的回复:
该警告告诉您常量
RAILS_ENV
已被弃用,而不是环境变量ENV["RAILS_ENV"]
。如果您克隆 Rails 存储库并查看 v3.0.4 标记,然后搜索RAILS_ENV
,您将看到 Rails 本身使用ENV["RAILS_ENV"]
在几个地方。必须来自应用程序中的其他位置。
Dup of my response in the GitHub issue:
That warning is telling you that the constant
RAILS_ENV
is deprecated, not the environment variableENV["RAILS_ENV"]
. If you clone the Rails repo and check out the v3.0.4 tag, and search forRAILS_ENV
, you'll see that Rails, itself, usesENV["RAILS_ENV"]
in several places.Must be coming from somewhere else in your app.
必须是 Rails 3.0.4 中的新增内容。这应该有效:
删除
ENV["RAILS_ENV"] || = 'test'
from spec/spec_helper.rb查找
require rspec/rails
行。紧随其后添加
::Rails.env ||= 'test'
。在 rspec-rails 跟踪器上提出问题是个好主意,因为这需要改变。
Must be new in Rails 3.0.4. This ought to work:
Remove the
ENV["RAILS_ENV"] || = 'test'
from spec/spec_helper.rbLook for the
require rspec/rails
line.Add
::Rails.env ||= 'test'
immediately after it.It would be a good idea to open an issue on the rspec-rails tracker, as this is going to need changing.