开发者

Spork, RSpec and database_cleaner destroying development database

开发者 https://www.devze.com 2023-04-10 02:22 出处:网络
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 a开发者_Stac

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 a开发者_StackOverflow社区dding 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消