Rspec is using my development DB

2020-05-27 03:45发布

问题:

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

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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.



回答5:

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.