Ruby-on-rails 如何确定我的 rails 是否在开发环境中而不是在测试环境中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15484004/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:39:02  来源:igfitidea点击:

How can I determine if my rails is in the development environment and not the test environment?

ruby-on-railstestingenvironment

提问by spierepf

I have some code that needs to run only if the rails app is in the development environment (i.e. $ rails server) but not in the test environment (i.e. $ rake test).

我有一些代码需要在 rails 应用程序处于开发环境(即 $ rails server)而不是在测试环境(即 $ rake test)中时运行。

When I try

当我尝试

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

the code gets executed regardless of which environment I am in. I've even tried:

无论我在哪个环境中,代码都会被执行。我什至尝试过:

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

but no love.

但没有爱。

What should I be doing instead?

我应该怎么做?

回答by Mori

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.

...确认环境是您认为的那样。