简体   繁体   中英

How to configure RSPEC to never run on RAILS_ENV production

I was trying to run some test because of changes I did...so I ran RAILS_ENV=production bundle exec rspec

which was something very stupid to do since rspec (the tests) were configured to truncate all the tables and this was exactly what happened on PRODUCTION !! and you can imagine the consequences

Is there a way to configure rspec to never run when RAILS_ENV=production so this can never happen to any one.

What other advice or good practices can be applied to avoid this kind of mistakes

UPDATE: I created a ISSUE for the rspec-rails team and they just commited a change that fixes this problem https://github.com/rspec/rspec-rails/pull/1383/files

In your Gemfile , refer to anything test-specific (like rspec-rails or rspec ) in the group for environments development and test only , instead of on the toplevel, eg:

group :development, :test do
  gem 'rspec-rails', '~> 3.0'
end

Then bundling without the development and test gems on your production machine. Without the rspec gem the tests would not have run. Add the --without switch when deploying:

bundle install --without development test

在您的spec_helper.rb ,在任何RAILS_ENV分配之前:

raise "Not in test" unless ENV['RAILS_ENV'] == "test"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM