8

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.

Steve Mitcham
  • 5,268
  • 1
  • 28
  • 56
  • what is your `database.yml` ? – Bohdan Sep 30 '11 at 11:07
  • Updated the question to include the information – Steve Mitcham Sep 30 '11 at 21:33
  • 3
    I also face same problem, currently I use `RAILS_ENV=test bundle exec rake spec` to prevent such thing happen. – subosito Jan 09 '12 at 10:14
  • @SteveMitcham I am facing the same problem.Did you find any solution to run "rake" and DatabaseCleaner to work as expected and not destroy development database? – p.matsinopoulos Sep 04 '12 at 17:12
  • I've had to move away from this project and haven't done rails for a while now. I don't believe I ever came across a solution. I haven't tried Nash's solution below. Let me know if it works out and I'll give him the credit for an answer. – Steve Mitcham Sep 08 '12 at 01:30
  • I'm also having this issue, I've tried moving `ENV["RAILS_ENV"] ||= 'test'` out of the `Spork.prefork`, but it doesn't seem to help. – Martinffx Apr 14 '13 at 09:58
  • I faced the same problem. In my case, I solved it moving `ENV["RAILS_ENV"] ||= 'test'` before `require File.expand_path("../../config/environment", __FILE__)` – hcarreras Feb 24 '15 at 16:57
  • Just FYI, I have moved off this project and will probably not be able to reproduce or verify an answer given. – Steve Mitcham Feb 24 '15 at 17:02

2 Answers2

3

Have you tried to move ENV["RAILS_ENV"] ||= 'test' out of the Spork.prefork block?

Are you sure that Spork caused your DB got uncleaned? If you're using RSpec's transactional fixtures, the only thing that could lead to such thing is using factories within before(:all) block. You can clean up data in after(:all) block and get rid of DatabaseCleaner.

BTW, if you're using truncation strategy, there's no need to run DatabaseCleaner.start.

Nash Bridges
  • 2,350
  • 14
  • 19
1

Is your RAILS_ENV explicitly set to "development"? If so, the default spec_helper will run the tests against the development DB. If you run a test from inside vim, or run rspec on the command-line, then it'll run the test against your development DB.

When I'm forced to use database_cleaner, I like to explicitly set RAILS_ENV to test in spec_helper to protect my development DB. Any time you're using something other than transactional fixtures, this is probably a good idea.

I note that you're using both transactional fixtures and database_cleaner. You don't need database_cleaner at all if your DB supports transactions. If you're using Mongo, or you are running capybara-webkit tests, then you'll need database_cleaner, and will want to disable transactional fixtures.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89