Search code examples
ruby-on-railsruby-on-rails-3unit-testingtestingobservers

How do I disable cache sweepers for testing purposes


I'm using a sweeper to watch a model and do some logging with snogmetrics:

class UserSweeper < ActionController::Caching::Sweeper
  observe User

  def after_create(user)
    km.identify(user.email)
  end
end

I'm doing this with a sweeper instead of an observer because snogmetrics needs the session in order to function properly.

However, while testing, snogmetrics utterly fails

NoMethodError: undefined method `identify' for nil:NilClass

How can I disable the sweeper for unit tests? Thanks!


Solution

  • I was able to use the no-peeping-toms gem to block observers during testing.

    NOTE: This also works for rake tasks. I had an issue with observers being called during a migration and this solved it.