Rails minitest, database cleaner how to turn use_transactional_fixtures = false

自闭症网瘾萝莉.ら 提交于 2019-12-20 02:56:19

问题


i would like to disable use_transactional_fixtures = false in ministest to catch after_commit callback. What and where should i set-up?


回答1:


You have a few options. One is to create a test without transactional fixtures and hope that the changes you make to the test database isn't going to break any other tests.

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

Another option you have is to keep the transactional fixtures, but invoke the after_commit callback manually.

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

And yet another option is to move the logic out of the after_commit callback into a new object, where you can write proper tests for it without relying on the callbacks to be invoked.



来源:https://stackoverflow.com/questions/20488758/rails-minitest-database-cleaner-how-to-turn-use-transactional-fixtures-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!