4

Rails 4 アプリを持っていて、トランザクション フィクスチャを使用しています。統合仕様の実行中に after_commit フックをトリガーしたいと考えています。

もちろん、トランザクション フィクスチャを使用する場合、実際にはコミットは発生しません。そのため、Rails 3 には次のパッチがありました: https://gist.github.com/charleseff/1305285で、gem に作成されました: 'test_after_commit'

Rails 4 では active_record の内部が大幅に変更されたため、Rails 3 のパッチを移植することはできません。

Rails 4でこの問題を解決した人はいますか?

4

1 に答える 1

3

次のコードを spec/support/helpers/test_after_commit.rb に入れることでこれを解決しました

require 'active_record/connection_adapters/abstract/transaction'
module ActiveRecord
  module ConnectionAdapters
    class SavepointTransaction < OpenTransaction
      def perform_commit_with_transactional_fixtures
        commit_records if number == 1
        perform_commit_without_transactional_fixtures        
      end

      alias_method_chain :perform_commit, :transactional_fixtures
    end
  end
end

https://gist.github.com/cmaitchison/5168104

于 2013-03-15T07:38:53.313 に答える