Posted 9 days ago. Visible to the public.

Rails < 5: How to get after_commit callbacks fired in tests

If you use transactional_fixtures or the database_cleaner gem with strategy :transaction, after_commit callbacks will not be fired in your tests.

Rails 5

Rails 5 has a fix for this issue and no further action is needed.

Rails 3+

Add the gem test_after_commit to your test group in the Gemfile and you are done. You don't need to change the database strategy to deletion (which might lead to longer test runs).


Most often a after_commit is the only right choice. Sending emails, performing async sidekiq workers and many things more are kind of broken with an after_save callback.

Things which will break if you replace an after_save with an after_commit callback:

  • attribute_changed? will return true in the after_save callback and false in the after_save callback. Use something like previous_changes.key?(:attribute) instead.
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Emanuel De
Last edit:
9 days ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more