Skip to content
Test existence of your Rails callbacks without having to call them
Branch: master
Clone or download
dvmonroe and jdliss Resolves #18 (#24)
* allow for array arg on callbacks
update readme
Latest commit b8a3680 Jun 7, 2016
Type Name Latest commit message Commit time
Failed to load latest commit information.
gemfiles Resolves #18 (#24) Jun 7, 2016
lib Resolves #18 (#24) Jun 7, 2016
.gitignore . ignoring all lockfiles May 22, 2014
.travis.yml Re-work .travis config for jruby problems May 23, 2016
Appraisals Resolves #18 (#24) Jun 7, 2016 . initial commit Nov 19, 2012
MIT-LICENSE . README update Apr 17, 2014 . initial commit Nov 19, 2012
shoulda-callback-matchers.gemspec Resolves #18 (#24) Jun 7, 2016

#Shoulda Callback Matchers Gem VersionBuild Status Code Climate Dependency Status

Matchers to test before, after and around hooks(currently supports method and object callbacks):


Method Callbacks:

describe Post do
  it { callback(:count_comments).before(:save) }
  it { callback(:post_to_twitter).after(:create) }
  it { callback(:evaluate_if_is_should_validate).before(:validation) }
  it { callback(:add_some_convenience_accessors).after(:find) }

  # with conditions

  it { callback(:assign_something).before(:create).if(:this_is_true) }
  it { callback(:destroy_something_else).before(:destroy).unless(:this_is_true) }

describe User do
  it { is_expected.not_to callback(:make_email_validation_ready!).before(:validation).on(:update) }
  it { callback(:make_email_validation_ready!).before(:validation).on(:create) }
  it { callback(:update_user_count).before(:destroy) }

Object Callbacks:

class CallbackClass
  def before_save

  def after_create

  def before_validation

  def after_find

describe Post do
  it { callback(CallbackClass).before(:save) }
  it { callback(CallbackClass).after(:create) }
  it { callback(CallbackClass).before(:validation) }
  it { callback(CallbackClass).after(:find) }

	# with conditions
  it { callback(CallbackClass).before(:create).if(:this_is_true) }
  it { callback(CallbackClass).after(:find).unless(:is_this_true?) }

describe User do
  it { is_expected.not_to callback(CallbackClass).before(:validation).on(:update) }
  it { callback(CallbackClass).before(:validation).on(:create) }
  # Only Rails > 3.2+
  it { callback(CallbackClass).before(:validation).on([:create, :update]) }
  it { callback(CallbackClass).before(:destroy) }

This will test:

  • the method call
  • method existence

Either on the model itself or on the callback object. Be aware that obviously this does not test the callback method or object itself. It makes testing via triggering the callback events (validation, save) unnecessary, but you still have to test the called procedure seperately.

In Rails 3 or 4 and Bundler, add the following to your Gemfile:

group :test do
  gem 'shoulda-callback-matchers', '~> 1.1.1'

This gem uses semantic versioning, so you won't have incompability issues with patches.

rspec-rails needs to be in the development group so that Rails generators work.

group :development, :test do
  gem "rspec-rails"

Shoulda will automatically include matchers into the appropriate example groups.


RSpec + Spring

undefined method `callback'

If you're getting this error, it's probably due to classes being redefined by Spring - currently this library does not accommodate for reloaded classes. The easiest fix is to load the matchers into the test library config in your rails_helper.rb:

RSpec.configure do |config|


This gem is maintained by me and its contributors, Shoulda is maintained and funded by thoughtbot

Contributors & Contributions

  • @pvertenten (callback objects)
  • @johnnyshields (bugfixes)
  • @esbarango (README updates)
  • @yuku-t (Rails 4.2 Support)

Let's make this gem useful, send me a PR if you've discovered an issue you'd like to fix!


Shoulda is Copyright © 2006-2014 thoughtbot, inc. Callback Matchers is Copyright © 2014 Beat Richartz It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

You can’t perform that action at this time.