Rspec is using my development DB

Relates to Rails 4.2.1 which is the latest stable version as of this post

Im still trying to solve this. However as a "stop-gap" you can prepend your rspec commands with an environment declaration "RAILS_ENV=test".

For example: to run a test you would write:

RAILS_ENV=test rspec path/to/test_spec.rb

You could alias rspec as "RAILS_ENV=test rspec" but that would just be hiding the problem and as such I personally haven't done this ... yet.

Ill be sure to update this thread with a solution as soon as I find one as none of the above as helped me at all.


I had a similar issue and despite having replaced ENV["RAILS_ENV"] = 'test' with Rails.env = 'test' in spec_helper.rb, I just have to manually specify RAILS_ENV=test when I run the command for it to work. Look here, but do try the Rails.env thing first:

Bundle exec rake spec and custom rake tasks


I randomly had $DATABASE_URL defined in my .bashrc file to point directly to my development database. Took me a few hours to find that.


I don't know if any of you is still having the issue, but for me moving

ENV["RAILS_ENV"] ||= 'test'

from rails_helper.rb to the top of spec_helper.rb fixed it. spec_helper.rb does a few things before loading rails_helper.rb, and something probably touches the database during that time.