Detect that code run by RSpec, Ruby
Asked Answered
K

2

6

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.

Keane answered 24/7, 2014 at 11:32 Comment(3)
Do you mean like the line-by-line coverage report provided by rcov?Nowlin
@MarkThomas I'm not sure what you are asking. I am not using rcov, no. I have developed a monkey-patched thing that sniffs every exception generated and stores this for later analysis.Keane
Sorry, I misread your question. Thought you wanted to know which code (i.e. which lines of code) were executed.Nowlin
V
16

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.

Vaporizer answered 24/7, 2014 at 11:48 Comment(0)
O
0

The global variable $PROGRAM_NAME contains the name of the ruby program being run. You can check to see if it ends with /rspec to determine if the code is being tested with rspec like this:

do_something unless $PROGRAM_NAME.end_with?('/rspec')
Ochre answered 28/8 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.