Obromios
Obromios

Reputation: 16393

How to set a constant from the command line when running rspec

With some rails commands, it is possible to set a constant from the command line e.g.

RAILS_ENV=production rails server

I want to do something similar with rspec; I want to set a constant from the command line that can be used to change some tests, e.g.

TEST_COLOR=red rspec spec

and then in a test access the value of TEST_COLOR. Is there a way to do this?

Upvotes: 0

Views: 217

Answers (1)

Ursus
Ursus

Reputation: 30071

Just try to use that variable in your code with ENV['TEST_COLOR'] and see. I mean, you can even write a one line script which makes use of environment variables and it works out of the box, I tried. So I'm reasonably confident this works even in rspec.

One suggestion: given that could be not clear to someone else that rspec needs that environment variable, maybe use ENV.fetch('TEST_COLOR', 'default value'), where 'default value' is a reasonable default, so that if anyone runs rspec without that variable, everything is fine anyway.

Upvotes: 2

Related Questions