I\'m using Rspec for testing. However, it uses my development DB instead of my test DB. How can this occur?
I simply use rspec to run my tests: don:my_project_root
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 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
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 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.
I had this issue and felt that moving things from rails_helper.rb
to spec_helper.rb
wasn't addressing the core issue. Why was rspec loading spec_helper before and/or without rails_helper.rb
?? Then it dawned on me to check .rspec
and lo and behold it was improperly set to:
--require spec_helper
Change that to
--require rails_helper
and it might just fix your issue without having to move stuff from rails_helper to spec_helper.