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

时间:2013-12-10 07:44:24

标签: ruby-on-rails minitest database-cleaner

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

1 个答案:

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