问题描述
我有一个简单的新 Rails 4 应用程序,它在我运行 rake test:units
时破坏了开发数据库,即使我已经在 test_helper.rb 中设置了 RAILS_ENV.我没想到.以下是重现它的简单步骤.
I have simple, new Rails 4 app which clobbers the development database when I run rake test:units
, even though I've set the RAILS_ENV in test_helper.rb. I wouldn't have expected that. Here are the simple steps to reproduce it.
我有 Ruby 2.0.0p247 和 Rails 4.0.1.
I have Ruby 2.0.0p247 and Rails 4.0.1.
rails new foo
rails generate scaffold gadget
rake db:migrate
我将 test/models/gadget_test.rb 编辑成这样:
I edit test/models/gadget_test.rb to look like this:
require 'test_helper'
class GadgetTest < ActiveSupport::TestCase
test "the env" do
assert_equal "test", Rails.env
end
end
并且我已经编辑了来自
ENV["RAILS_ENV"] ||= "test"
成为
ENV["RAILS_ENV"] = "test"
即便如此,当测试调用 rake test:units
时,它还是失败了:
Even so, when the tests invoke rake test:units
it fails:
1) Failure:
GadgetTest#test_the_env test/models/gadget_test.rb:5]:
Expected: "test"
Actual: "development"
对于我设置的较旧的(Rails 3)应用程序,我可以指望这种默认设置为测试环境.我错过了什么?
With older (Rails 3) apps I've set up, I could count on this defaulting to the test environment. What am I missing?
推荐答案
真正的答案:在我的配置文件引用的 shell 脚本中存在一个杂散的 export RAILS_ENV="development"
.所以这是一个座位到键盘的接口故障,直接来自 2013 年 10 月 31 日在这个线程中:https://github.com/rails/rails/issues/7175
The real answer: there was a stray export RAILS_ENV="development"
in a shell script referenced by my profile. So this is a seat-to-keyboard interface failure, directly from Oct 31, 2013 in this thread: https://github.com/rails/rails/issues/7175
您需要做的就是去掉它,问题就会消失.
All you need to do is take that out and the problem goes away.
这篇关于Rails 4,新应用:为什么要在开发环境中运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!