我的Rails 3.1應用程序中有以下spec_helper.rb文件。我正在使用Spork更快地加載測試環境。在將Spork添加到組合之前,我所有的測試都可以正常工作。在添加spork之後,測試數據庫在測試運行之間沒有得到適當的清除,這導致了我的一些期望。Spork,RSpec和database_cleaner破壞開發數據庫
按照其他說明,我將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進入工作之前(:每個)部分,但是這會減慢測試運行顯着。
什麼是你的'database.yml'? – Bohdan
更新了問題以包含信息 –
我也面臨同樣的問題,目前我使用'RAILS_ENV =測試包exec rake spec'來防止發生這種情況。 – subosito