Skip to content

Repository files navigation

Make after_commit callbacks fire in tests for Rails 3+ with transactional_fixtures = true.

Deprecation this is no longer needed on rails 5.0+ rails/rails#18458

Install

gem install test_after_commit
# Gemfile (never include in :development !)
gem 'test_after_commit', :group => :test

Usage

Test that the methods get called or the side-effect of the methods, something like:

classCar < ActiveRecord::Baseafter_commit:foo,:on=>:updatedeffoo
$foo =1endend
...
it"sets $foo on commit"do
$foo.should == nilcar.save!
$foo.should == 1end

Temporary disable after commit hooks

In your test_helper, you can specify the default

TestAfterCommit.enabled = true

Then use blocks in your tests to change the behavior:

TestAfterCommit.with_commits(true) do
my_tests
end
TestAfterCommit.with_commits(false) do
my_tests
end

TIPS

Author

Inspired by https://gist.github.com/1305285

Michael Grosser
michael@grosser.it
License: MIT
Build Status

About

Make after_commit callbacks fire in tests for Rails 3+ with transactional_fixtures = true.

Resources

Stars

237 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages