Spork, RSpec and database_cleaner destroying development database
Asked Answered
T

2

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.

Thun answered 29/9, 2011 at 19:3 Comment(8)
what is your database.yml ?Provisional
Updated the question to include the informationThun
I also face same problem, currently I use RAILS_ENV=test bundle exec rake spec to prevent such thing happen.Ambience
@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?Adman
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.Thun
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.Chaetopod
I faced the same problem. In my case, I solved it moving ENV["RAILS_ENV"] ||= 'test' before require File.expand_path("../../config/environment", __FILE__)Millwater
Just FYI, I have moved off this project and will probably not be able to reproduce or verify an answer given.Thun
S
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.

Samaveda answered 20/2, 2012 at 10:4 Comment(0)
T
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.

Teferi answered 23/11, 2012 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.