Rails设置后Minitest不一致的数据

Rails Minitest inconsistent data after setup

我一直被以下问题困扰着,但没有成功: 尝试测试 rake 任务是否正确填充一些 tables,我有以下配置:

class InitialDataTest < ApplicationSystemTestCase
  def setup
    DatabaseCleaner.clean_with :truncation, only: %w[centres projects departments]
    System::Application.load_tasks
    Rake::Task['populate_lookup_tables:commit'].invoke

    sign_in users(:test_user)
  end

  test 'projects are created' do
    puts " Projects: #{Project.count}"
  end

  test 'centres are created' do
    puts "Centres: #{Centre.count}"
  end

  test 'departments are created' do
    puts " Departments: #{Department.count}"
  end
end

我的 rake 任务实际上按预期工作(例如,在开发中)但是,对于测试,似乎只有 1 table 被填充:

Running via Spring preloader in process 15362
Started with run options --seed 17058

:: Centre populated successfully
:: Project populated successfully
:: Department populated successfully
>> OperationType is not empty
>> DocumentType is not empty
>> RecordErrorType is not empty

Puma starting in single mode...
* Version 3.9.1 (ruby 2.3.3-p222), codename: Private Caller
* Min threads: 0, max threads: 1
* Environment: test
* Listening on tcp://0.0.0.0:51272
Use Ctrl-C to stop
Departments: 5
Centres: 0
Projects: 0
3/3[========================================================] 100% Time: 00:00:03, Time: 00:00:03

Finished in 3.88633s
3 tests, 0 assertions, 0 failures, 0 errors, 0 skips
Coverage report generated for Unit Tests to <My Stuff> (28.45%) covered.

默认情况下,Rake 任务只能在给定上下文中调用一次。如果您想再次 运行 它,您需要先调用 Rake::Task['populate_lookup_tables:commit'].reenable