I have the following spec_helper.rb file in my Rails 3.1 application. I am using Spork to load the environment faster for testing. All my tests worked prior to adding Spork to the mix. After adding spork, the test database was not getting properly cleared between test runs which threw off some of my expectations.
Following other instructions, I added database_cleaner to the mix with the code listed below; however, now, the development database is getting cleaned up as well as the test database. The ENV["RAILS_ENV"] call is returning test during this call.
Is there a way to explicitly limit the call for DatabaseCleaner.clean_with(:truncation) to only affect the test database?
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
Update: Here is my database.yml file
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
Also, I have worked around the basic problem by moving the clean_with call into the before(:each) section, but this slows down the test runs significantly.
database.yml
? – ProvisionalRAILS_ENV=test bundle exec rake spec
to prevent such thing happen. – AmbienceENV["RAILS_ENV"] ||= 'test'
out of theSpork.prefork
, but it doesn't seem to help. – ChaetopodENV["RAILS_ENV"] ||= 'test'
beforerequire File.expand_path("../../config/environment", __FILE__)
– Millwater