如何确定我的 Rails 是否在开发环境中,而不是在测试环境中?

我有一些代码,只有当 Rails 应用程序在开发环境中(例如 $ails 服务器)而不在测试环境中(例如 $rake 测试)时才需要运行。

当我尝试的时候

if Rails.env.development?
dont run me during testing
end

不管我在哪个环境中,代码都会被执行。我甚至试过:

if Rails.env.development? and not Rails.env.test?
NO, REALLY, DONT RUN ME DURING TESTING
end

但没有爱。

那我应该做什么呢?

67486 次浏览

It looks like you're calling it correctly. Perhaps the problem is that the environment is named differently somewhere. Try in the console:

> Rails.env
=> "development"
> Rails.env.development?
=> true
> Rails.env.test?
=> false

...to confirm that the environment is what you think it is.