【问题标题】:Rails minitest, database cleaner how to turn use_transactional_fixtures = falseRails minitest,数据库清理器如何将use_transactional_fixtures = false
【发布时间】:2013-12-10 07:44:24
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您有几个选择。一种是创建一个没有事务性固定装置的测试,并希望您对测试数据库所做的更改不会破坏任何其他测试。

    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 回调中移出到一个新对象中,您可以在其中为其编写适当的测试,而无需依赖要调用的回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多