Rails minitest,数据库清理器如何打开use_transactional_fixtures = false [英] Rails minitest, database cleaner how to turn use_transactional_fixtures = false

查看:81
本文介绍了Rails minitest,数据库清理器如何打开use_transactional_fixtures = false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ministest中禁用use_transactional_fixtures = false来捕获after_commit回调.我应该在哪里设置?

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

推荐答案

您有一些选择.一种方法是创建一个没有事务处理手段的测试,并希望您对测试数据库所做的更改不会破坏任何其他测试.

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

您还有另一个选择,就是保留交易固定装置,但手动调用after_commit回调.

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

还有另一种选择是将逻辑从after_commit回调中移到新对象中,您可以在其中为它编写适当的测试,而不必依赖于要调用的回调.

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.

这篇关于Rails minitest,数据库清理器如何打开use_transactional_fixtures = false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆