我想在ministest中禁用use_transactional_fixtures = false来捕获after_commit回调。我应该在什么地方和哪里设置?
答案 0 :(得分: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
回调移到一个新对象中,在那里你可以为它编写适当的测试而不依赖于要调用的回调。