Chris Aitchison
Chris Aitchison

Reputation: 4654

Testing an after_commit hook in Rails 4 with transactional fixtures enabled

I've got a Rails 4 app and am using transactional fixtures, and want my after_commit hooks to trigger while running my integration specs.

Of course, no commits actually happens when you use transactional fixtures, so in Rails 3 there was this patch: https://gist.github.com/charleseff/1305285, and it was made into a gem: 'test_after_commit'

Rails 4 has changed the internals of active_record enough so that the Rails 3 patches cannot be ported.

Has anyone solved this issue in Rails 4?

Upvotes: 4

Views: 2183

Answers (1)

Chris Aitchison
Chris Aitchison

Reputation: 4654

I solved this by putting the following code in 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

Upvotes: 3

Related Questions