This project is archived and is in readonly mode.

#835 ✓resolved
Mark Cornick

rescue_from handlers are not being honored in functional tests

Reported by Mark Cornick | August 14th, 2008 @ 10:37 PM

rescue_from handlers are not being honored in functional tests since commit 90c930f45c5c6766306929241462ffff8f67b86e.

Consider the following trivial controller and test, from a rails application created with the current edge code:


class ThingsController < ApplicationController
  rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

  def show
    @thing = Thing.find(params[:id])
  end

  # ...

  private
  def record_not_found
    flash[:error] = 'That thing could not be found'
    redirect_to things_url
  end
end

require 'test_helper'

class ThingsControllerTest < ActionController::TestCase
  test "show with invalid id should set flash and redirect" do
    get :show, :id => nil
    assert_equal 'That thing could not be found', flash[:error]
    assert_redirected_to things_url
  end
end

This functional test should pass because rescue_from should handle the ActiveRecord::RecordNotFound exception raised by Thing.find(nil). Instead, the exception falls through to the test, which aborts before hitting the assertions:


  1) Error:
test_show_with_invalid_id_should_set_flash_and_redirect(ThingsControllerTest):
ActiveRecord::RecordNotFound: Couldn't find Thing without an ID

The attached patch reverts a removal of rescue_action_with_handler that happened in commit 90c930f45c5c6766306929241462ffff8f67b86e. With this patch, the above test passes. However, I am not sure that this patch is the correct solution.

Comments and changes to this ticket

  • Matt Swasey

    Matt Swasey August 14th, 2008 @ 10:58 PM

    • Tag changed from 2.1, actionpack, bug, controller, patch, tests to actionpack, bug, controller, edge, patch, tests

    +1

    Ah ha! I just had this happen, thanks for the patch!

  • Pratik

    Pratik August 21st, 2008 @ 12:00 PM

    • State changed from “new” to “invalid”
    • Tag changed from actionpack, bug, controller, edge, patch, tests to 2.1, actionpack, bug, controller, edge, patch, tests

    if you remove overridden 'rescue_action' in your functional test, it should work as expected.

  • Mark Cornick

    Mark Cornick August 21st, 2008 @ 12:58 PM

    
    # Re-raise errors caught by the controller.
    class ThingsController; def rescue_action(e) raise e end; end
    

    The test posted above, as well as the tests in my current application that exposed this bug, do not contain these lines, and do not override rescue_action.

    In other words, this is not the same issue as #664.

  • Mark Cornick

    Mark Cornick August 21st, 2008 @ 12:59 PM

    Lighthouse ate the text before my code block, which read:

    "I assume you are referring to lines such as the following, which are present in tests generated by very old versions of the scaffold generator:"

  • Pratik

    Pratik August 21st, 2008 @ 01:04 PM

    Oops. My bad. Could you please attach a failing test case or steps to reproduce ?

    Thanks.

  • Mark Cornick

    Mark Cornick August 21st, 2008 @ 04:23 PM

    The attachment has a controller, functional test, and enough Rails infrastructure to demonstrate the bug.

    Steps to reproduce: 1) unzip the code 2) install rails 2.1.0 gem or freeze rails 2.1.0 into vendor 3) rake test:functionals

    Expected: test_show_with_invalid_id_should_set_flash should pass Actual: test_show_with_invalid_id_should_set_flash passes

    4) git clone current edge rails into vendor 5) rake test:functionals

    Expected: test_show_with_invalid_id_should_set_flash should pass Actual: test_show_with_invalid_id_should_set_flash raises an unhandled ActiveRecord::RecordNotFound exception and aborts

  • Pratik

    Pratik August 21st, 2008 @ 04:45 PM

    • State changed from “invalid” to “incomplete”
    • Assigned user set to “Pratik”
  • Pratik

    Pratik September 30th, 2008 @ 01:33 AM

    • State changed from “incomplete” to “open”
    • Milestone cleared.
    • Tag changed from 2.1, actionpack, bug, controller, edge, patch, tests to 2.1, actionpack, bug, controller, edge, patch, tests

    ActionController::TestCase is overriding rescue_action, causing this issue.

  • Repository

    Repository October 4th, 2008 @ 06:47 PM

    • State changed from “open” to “resolved”

    (from [5e3517ea7b9fbd460f772bffc9212d882011f2bc]) Ensure rescue_from handlers are respected inside tests. [#835 state:resolved]

    Note : If you're not using rescue_from, you should overrider rescue_action_without_handler() method and not rescue_action(). Afterwards, you can set request.remote_addr to a non "0.0.0.0" value for testing the overridden behavior. http://github.com/rails/rails/co...

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages