Rescue in failing controller spec

Public, Testing / TDD

harrylevine

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
  flash.now[: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

    begin
      @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]}!")
        @change_request.send_result(current_user)

        send_time_off_connect_message(@change_request)

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

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.