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
gem install test_after_commit
# Gemfile (never include in :development !)
gem 'test_after_commit', :group => :test
Test that the methods get called or the side-effect of the methods, something like:
class Car < ActiveRecord::Base
after_commit :foo, :on => :update
def foo
$foo = 1
end
end
...
it "sets $foo on commit" do
$foo.should == nil
car.save!
$foo.should == 1
end
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
- hooks do not re-raise errors (with or without this gem) use after_commit_exception_notification
Inspired by https://gist.github.com/1305285
- James Le Cuirot
- emirose
- Brad Gessler
- Rohan McGovern
- lsylvester
- Tony Novak
- Brian Palmer
- Oleg Dashevskii
- Jonathan Spies
- Nick Sieger
Michael Grosser
[email protected]
License: MIT