Rspec 正在使用我的开发数据库 [英] Rspec is using my development DB
问题描述
我正在使用 Rspec 进行测试.但是,它使用我的开发数据库而不是我的测试数据库.这怎么会发生?
I'm using Rspec for testing. However, it uses my development DB instead of my test DB. How can this occur?
我只是使用 rspec 来运行我的测试:don:my_project_root $ rspec
I simply use rspec to run my tests: don:my_project_root $ rspec
其他问题中的常见错误似乎只与 Rails 3 相关,或者已经过时(使用不再与 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).
下面是我的 spec_helper.rb.
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
数据库.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
推荐答案
我不知道你们中是否还有人遇到这个问题,但我很感动
I don't know if any of you is still having the issue, but for me moving
ENV["RAILS_ENV"] ||= 'test'
从 rails_helper.rb
到 spec_helper.rb
的顶部修复了它.spec_helper.rb
在加载 rails_helper.rb
之前做了一些事情,在此期间可能有一些东西会触及数据库.
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.
这篇关于Rspec 正在使用我的开发数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!