我使用Rspec进行测试。但是,它使用我的开发数据库而不是我的测试数据库。怎么会发生这种情况?
我只是使用rspec来运行我的测试:don:my_project_root $ rspec
其他问题的常见错误似乎只与Rails 3有关,或者已经过时了(使用不再用于rspec的命令)。
以下是我的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
答案 0 :(得分:31)
我不知道你们中是否有人仍然有这个问题,但对我而言
ENV["RAILS_ENV"] ||= 'test'
从rails_helper.rb
到spec_helper.rb
的顶部修复了它。 spec_helper.rb
在加载rails_helper.rb
之前会执行一些操作,并且在此期间可能会触及数据库。
答案 1 :(得分:2)
我遇到了类似的问题,尽管在ENV["RAILS_ENV"] = 'test'
中将Rails.env = 'test'
替换为spec_helper.rb
,但我必须在运行命令时手动指定RAILS_ENV=test
才能使其正常工作。看看这里,但首先尝试Rails.env的事情:
答案 2 :(得分:1)
与Rails 4.2.1相关,这是此帖后的最新稳定版
我还在努力解决这个问题。但是,作为“止损”,您可以在环境声明“RAILS_ENV = test”之前添加rspec命令。
例如:运行测试你会写:
RAILS_ENV=test rspec path/to/test_spec.rb
你可以将rspec别名为“RAILS_ENV = test rspec”,但这只会隐藏问题,因此我个人还没有这样做......
我发现只要找到一个没有上述帮助我的解决方案,我就一定要用解决方案更新这个帖子。
答案 3 :(得分:1)
我在$DATABASE_URL
文件中随机定义了.bashrc
,直接指向我的开发数据库。花了几个小时才找到它。
答案 4 :(得分:1)
我遇到了这个问题,并感到将事情从rails_helper.rb
转移到spec_helper.rb
并没有解决核心问题。为什么rspec在rails_helper.rb
之前和/或之前不加载spec_helper?然后我突然想到要检查.rspec
,然后瞧瞧它被错误地设置为:
--require spec_helper
将其更改为
--require rails_helper
它也许可以解决您的问题,而不必将东西从rails_helper移到spec_helper。