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

Ruby on-RailsTestingEnvironment

Ruby on-Rails Problem Overview


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).

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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionspierepfView Question on Stackoverflow
Solution 1 - Ruby on-RailsMoriView Answer on Stackoverflow