Rescue in failing controller spec

Public, Testing / TDD


Created: Apr 01, 2016     Updated: Apr 01, 2016

I was getting a failing controller spec, and the failure message was not helpful at all, nor were any of the debugging approaches. A coworker recommended that I check the controller method itself to see if there is a rescue block in there. Low and behold, there was.

But, it was rescuing in a way that is beneficial in the UI, but not during testing.

rescue[:errors] = format_all_messages(@change_request)
  render :action => "show"

My coworker recommending that I replace the rescue block with this block, because it gives information on why the spec is failing, when I run the specs:

rescue StandardError => e
  raise e.inspect

Here is how the whole method looked. Note that I commented out the original rescue clause, replacing it with the above:

  def dispose
    authorize! :dispose, ChangeRequest

      @change_request = ChangeRequest.find(params[:id])
      @change_request.attributes = params[:change_request]
      @change_request.disposed_of_by = current_user

      if @change_request.send("#{params[:state]}!")


        redirect_to change_requests_path
      else[:errors] = format_all_messages(@change_request)
        render :action => "show"
    rescue StandardError => e
      raise e.inspect
    # rescue
      #[:errors] = format_all_messages(@change_request)
      # render :action => "show"

The information generated from this new rescue block enabled me to get the spec passing, at which point I swapped it back out of the controller method.