0

I have an Rails app, with some initializer code which must be executed when the app is running in development mode. However, this initializer code must not be executed when running tests.

I have established that

$ rake test

causes the app to be run in development mode, which invokes the initializer code and therefore breaks my tests. This is expected behaviour apparently (see: https://github.com/rails/rails/issues/9801).

What is the correct command to run my Rails app tests without starting the app in development mode?

4

3 回答 3

1

你的 test_helper.rb 文件看起来像默认的吗?它应该从以下开始:

ENV["RAILS_ENV"] = "test"
于 2013-03-19T14:08:44.070 回答
0

你需要在 test_helper.rb 文件的开头有一行说

Rails.env = "test"

你不能使用

ENV["RAILS_ENV"] = "test"

因为它将无法清除从 Rails.env 调用返回的缓存值。

于 2014-03-04T03:57:06.500 回答
0

尝试使用显式环境运行 rake 任务:

rake test:units RAILS_ENV=test

根据我的经验,如果您不指定环境,则假定为开发。尽管测试数据库仍将夹具数据插入其中,但出于某种原因仍会引用开发环境中的内容。

于 2013-03-19T14:02:35.273 回答