Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

rescue_from handlers are not being honored in functional tests

#835

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.

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

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Matt Swasey
    Matt Swasey
    • 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!

    August 14th, 2008 @ 10:58 PM

  2. Pratik
    Pratik
    • 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.

    August 21st, 2008 @ 12:00 PM

  3. Mark Cornick
    Mark Cornick
    
    # 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 rescue_from changes causing issues.

    August 21st, 2008 @ 12:58 PM

  4. Mark Cornick
    Mark Cornick

    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:"

    August 21st, 2008 @ 12:59 PM

  5. Pratik
    Pratik

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

    Thanks.

    August 21st, 2008 @ 01:04 PM

  6. Mark Cornick
    Mark Cornick

    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

    August 21st, 2008 @ 04:23 PM

  7. Pratik
    Pratik
    • State changed from invalid to incomplete
    • Assigned user set to Pratik

    August 21st, 2008 @ 04:45 PM

  8. Pratik
    Pratik
    • 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.

    September 30th, 2008 @ 01:33 AM

  9. Repository
    Repository
    • State changed from open to resolved

    (from [5e3517ea7b9fbd460f772bffc9212d882011f2bc]) Ensure rescue_from handlers are respected inside tests. [#835 rescue_from handlers are not being honored in functional tests 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...

    October 4th, 2008 @ 06:47 PM