Rails 3.1 アプリケーションに次の spec_helper.rb ファイルがあります。私は Spork を使用して、テストのために環境をより高速にロードしています。Spork をミックスに追加する前に、すべてのテストが機能していました。spork を追加した後、テスト実行の間にテスト データベースが適切にクリアされず、私の期待の一部が失われました。
他の指示に従って、database_cleaner を以下にリストされているコードとの組み合わせに追加しました。ただし、現在、開発データベースとテスト データベースがクリーンアップされています。ENV["RAILS_ENV"] 呼び出しは、この呼び出し中にテストを返しています。
DatabaseCleaner.clean_with(:truncation) の呼び出しを明示的に制限して、テスト データベースのみに影響を与える方法はありますか?
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda/matchers/integrations/rspec'
require 'database_cleaner'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :mocha
config.formatter = 'documentation'
config.use_transactional_fixtures = true
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
FactoryGirl.reload
end
更新: ここに私の database.yml ファイルがあります
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
また、clean_with 呼び出しを before(:each) セクションに移動することで基本的な問題を回避しましたが、これによりテストの実行が大幅に遅くなります。