Rspec is using my development DB
rspec, ruby-on-rails, ruby-on-rails-4
Solution
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.
Problem
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 $ rspec` It seems like the common mistakes from other questions are only relevant to Rails 3 or are pretty outdated (using commands you no longer use with rspec). Below is my spec_helper.rb. ``` ENV["RAILS_ENV"] = 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require "capybara/rspec" require 'database_cleaner' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. # ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) RSpec.configure do |config| # ## Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures # config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = false # commented-out during tim's tutorial # Rails cast tutorial config.before(:suite) do DatabaseCleaner.strategy = :truncation end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end # If true, the base class of anonymous controllers will be inferred # automatically. This will be the default behavior in future versions of # rspec-rails. config.infer_base_class_for_anonymous_controllers = false # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 config.order = "random" config.include FactoryGirl::Syntax::Methods config.include Capybara::DSL end ``` database.yml ``` development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000 ```