Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10862

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

Upvotes: 3

Views: 639

Answers (2)

Chris Heald
Chris Heald

Reputation: 62678

In your spec_helper.rb, before any RAILS_ENV assignment:

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

Upvotes: 3

infused
infused

Reputation: 24347

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, e.g.:

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

Upvotes: 5

Related Questions