Rspec-core: How to set default environment var for RSpec?

Created on 22 Aug 2016  ·  5Comments  ·  Source: rspec/rspec-core

What happend & What I want

I dont't wanna prefix a RAILS_ENV=development every single time

# work
RAILS_ENV=development rspec spec/routing/username_schoolname_spec.rb

# not working
# because in test environment
rspec spec/routing/username_schoolname_spec.rb


What I have tried

I tried put ENV["RAILS_ENV"] ||= 'development' in top of spec_helper.rb but not working

ENV["RAILS_ENV"] ||= 'development' 
require 'factory_girl_rails'
require 'support/factory_girl'
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.

I tried to use dovenv gem but still not working.



what should I do to set default environment to development?

Thanks :dancer:
:smile:

Thanks for your time~
RSpec is awesome!

Most helpful comment

This does not work with Rails 5.1.4 and rspec 3.7. It gladly ignores your ENV setting in the spec helper and will seemingly happily destroy your development database every time, leaving you feeling betrayed and disappointed. Some developers have been known to seek therapy.

All 5 comments

If you want to override RAILS_ENV for a specific test, or for all tests, you can simply do ENV["RAILS_ENV"] = "development" in that test, or at the top of your spec helper. The ||= will only override it if it's not set.

@samphippen Thank you !
By the way I saw your talk on Youtube, very funny.
:D

This does not work with Rails 5.1.4 and rspec 3.7. It gladly ignores your ENV setting in the spec helper and will seemingly happily destroy your development database every time, leaving you feeling betrayed and disappointed. Some developers have been known to seek therapy.

@wedgemartin if you'd like to report a problem, please open an issue in the rspec-rails repo, provide code that is not working how you expect, how you expect it to work, and what it is doing instead.

Have you try:

before(:each) do
     stub_const('ENV', 'NAME' => '')
end
Was this page helpful?
0 / 5 - 0 ratings