单元测试:如何测试 rails 迁移是否被调用?

Unit test: How to test if rails migrations being called?

我有一个运行 rails 迁移的方法。 (RAILS 应用程序安装自动化的一部分)。

我想测试此方法是否调用了 rails 迁移。我不想检查 运行 迁移的结果,因为那会测试 rails 迁移。对该方法进行单元测试意味着我想检查我的方法是否依次调用 rails 迁移。

如何在 rspec 中对该方法进行单元测试?

def run_migrations
  system('bin/rails db:migrate RAILS_ENV=development')
end

system('bin/rails db:migrate RAILS_ENV=development') return true,当它工作时。所以 expect(run_migrations).to eq true 就足够了。

我会存根该调用并检查该存根是否被调用。

before do
  allow(Kernel).to receive(:system).and_return(true)
end

it 'runs migrations' do
  instance.run_migrations # or however you trigger such that method to be called

  expect(Kernel).to have_received(:system).with('bin/rails db:migrate RAILS_ENV=development').once
end