[rspec-users] Issue when testing ActiveRecord after_commit callbacks

Dennis Kuczynski dennis.kuczynski at gmail.com
Thu Jun 28 19:11:19 UTC 2012

In an ActiveRecord model, I have an after_commit callback (:enqueue_job)
which places a job on my background processing queue (Sidekiq)

To test that the callback was firing when the database transaction
was committed, I was using:

object.should_receive(:enqueue_job) #should pass

Which seems to work.  However, to test that the test was valid, I attempted

object.should_not_receive(:enqueue_job) #should fail

But this did not fail.

I tracked this down to ActiveRecord's
DatabaseStatements' commit_transaction_records method, which ends up eating
the RSpec Negative Method Expectation Exception (which fails fast)

If the negative method expectation did not fail fast, the test would
probably work, but is there a better pattern for testing after_commit logic?

(This was with rspec-mocks 2.10.1.  use_transactional_fixtures was turned
off to enable the callback.)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120628/0d88f7fc/attachment.html>

More information about the rspec-users mailing list