Rails minitest,数据库更清洁如何转为use_transactional_fixtures = false

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

你有几个选择。 一种是在没有事务夹具的情况下创建测试,并希望您对测试数据库所做的更改不会破坏任何其他测试。

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回调移到一个新对象,在那里你可以为它编写适当的测试,而不依赖于要调用的回调。