rubyrspecenvironment-variables

Detect that code run by RSpec, Ruby


I would like to be able to know that my code is run under rspec or not. Is this possible?

The reason is that I am loading some error loggers that would be cluttered with deliberate errors (expect{x}.to raise_error) during testing.

I have looked at my ENV variable, and there is no (apparent) signs of a test environment variable.


Solution

  • Add at the beginning of your spec_helper.rb:

    ENV['RACK_ENV'] = 'test'
    

    Now you can check in your code whether the RACK_ENV is test or not.