小编Hyu*_*Kim的帖子

DatabaseCleaner.clean_with(:truncate) 不会重置自动递增的 id

我目前DatabaseCleaner在运行 PostgreSQL 的 Rails 项目中使用,并将其设置如下。

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation, { pre_count: true, reset_ids: true })
  end

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

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

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

  config.after(:each) do
    DatabaseCleaner.clean
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的一个 rails 测试套件中,我打印了一个实例的 id。我认为它应该是相对较小的数字,因为 clean_with(:truncate) 假设清除数据库并对其运行真空。但每次我运行测试时它都会增加。

测试通过,它使用什么序列并不重要。但为什么clean_with(:truncation)不以它应该的方式工作?

====== 编辑 ======

这在 RSpec 测试的范围内。我知道序列编号对性能没有影响,但对每个 :suite 进行昂贵的清理 (:truncation) 并使用廉价且快速的清理 (:transaction) 会。所以我想了解为什么clean_with(:truncation)在运行测试套件之前不为我重置 id 以获得干净的数据库状态。

ruby postgresql ruby-on-rails rspec-rails database-cleaner

6
推荐指数
1
解决办法
3681
查看次数