[rspec-users] Specs for controllers and rescue_from

Nick Hoffman nick at deadorange.com
Sat Nov 20 23:12:17 EST 2010


Hey guys. My ApplicationController rescues
Mongoid::Errors::DocumentNotFound errors like this:

class ApplicationController < ActionController::Base
  rescue_from Mongoid::Errors::DocumentNotFound,
    :with => :resource_not_found

  protected

  def resource_not_found(error)
    flash[:alert] = t('errors.resource_not_found')
    redirect_to root_url
  end
end

Obviously, I need specs for this. However, I can't figure out how this
should be specced.

Should I create a shared example group that expects the flash-alert
and redirect, and use it in every controller action that could
potentially raise this error? This seems right, but will be verbose.

Or, in each controller that could raise this error, should I create
one example group that raises this error and expects the flash-alert
and redirect? This seems right because the rescuing behaviour exists
within the controller rather than each action. However, it ignores the
possibility of an action rescuing the error.

Should I create a dummy controller with an action that raises this
error, and spec that? This would be akin to speccing
ApplicationController, though indirectly. This method was my first
inclination, but fails to provide specs for the other controllers that
inherit the rescuing behaviour.

How would you spec this? I'm all ears!
Nick


More information about the rspec-users mailing list