Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set default environment var for RSpec? #2315

Closed
1c7 opened this issue Aug 22, 2016 · 7 comments
Closed

How to set default environment var for RSpec? #2315

1c7 opened this issue Aug 22, 2016 · 7 comments

Comments

@1c7
Copy link

1c7 commented Aug 22, 2016

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 💃
😄

Thanks for your time~
RSpec is awesome!

@fables-tales
Copy link
Member

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.

@1c7
Copy link
Author

1c7 commented Aug 23, 2016

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

@wedgemartin
Copy link

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.

@myronmarston
Copy link
Member

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

@lucia-w
Copy link

lucia-w commented Sep 4, 2020

Have you try:

before(:each) do
     stub_const('ENV', 'NAME' => '')
end

@cmdr-pushpendra-singh
Copy link

@shiralwz Thanks it works for me

@Startouf
Copy link

Startouf commented Sep 9, 2021

Adding this at the top of my rails_helper (above requires)

ENV['SOME_ENV'] ||= 'some_value'

works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants