2

我想在 ministest 中禁用 use_transactional_fixtures = false 以捕获 after_commit 回调。我应该在哪里设置什么?

4

1 回答 1

5

你有几个选择。一种是创建一个没有事务性固定装置的测试,并希望您对测试数据库所做的更改不会破坏任何其他测试。

class SomethingTest < ActiveSupport::TestCase
  self.use_transactional_fixtures = false

  def test_something_with_after_commit
    # do work here, which will change your test database
  end
end

另一个选择是保留事务性固定装置,但手动调用 after_commit 回调。

class SomethingTest < ActiveSupport::TestCase
  def test_something_with_after_commit
    something = Something.new
    something.save
    something.after_commit
    # verify things happened as expected
  end
end

另一种选择是将逻辑从after_commit回调中移出到一个新对象中,您可以在其中为其编写适当的测试,而无需依赖要调用的回调。

于 2013-12-10T14:12:08.647 回答