سؤال

This config worked fine when I was using sqlite:

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    controller.stub(:should_navigate_user?).and_return(false) rescue ""
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end

When I switched to PostgreSQL I began getting errors when tests depended on the id number of entries in the database. I looked at the database during the tests and it was never cleaning the database.

If I switch to the following config:

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    DatabaseCleaner.clean
    controller.stub(:should_navigate_user?).and_return(false) rescue ""
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end

All of my tests pass, but I'm having to call clean before and after my methods tests?

I shouldn't have to clean the database before and after each test - right? I feel like I'm misunderstanding something somewhere. What am I failing to understand.

هل كانت مفيدة؟

المحلول 2

You used sqlite in non-persistent mode. PostgreSQL doesn't have such options, always persistent. Use drop database/create database to clean it complettely.

نصائح أخرى

I'm using postgres with the database cleaner. Its working well now, but I remember having trouble at first. I pasted my config below. You can stick this in your spec_helper.rb, or in another file such as /support/database_cleaner.rb

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

Also make sure you turn the transactional fixtures off in the spec_helper:

config.use_transactional_fixtures = false
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top