2

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.

4

2 回答 2

3

我正在使用带有数据库清理器的 postgres。它现在运行良好,但我记得一开始遇到了麻烦。我在下面粘贴了我的配置。您可以将其粘贴在您的 spec_helper.rb 或其他文件中,例如 /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

还要确保在 spec_helper 中关闭事务性装置:

config.use_transactional_fixtures = false
于 2013-08-15T04:30:25.897 回答
1

您在非持久模式下使用了 sqlite。PostgreSQL 没有这样的选项,总是持久的。使用 drop database/create database 彻底清理它。

于 2013-07-18T14:33:22.820 回答