This project is archived and is in readonly mode.

#1490 ✓stale
Ivan

assert_redirected_to is too strict

Reported by Ivan | November 28th, 2008 @ 12:48 PM | in 3.x

We've had several tests broken after the update to Rails 2.2 because of the new behavior of assert_redirected_to. The new version doesn't allow to match:


redirect_to '/categories/new'

or


redirect_to :action => 'new'

with:


assert_redirected_to :controller => 'categories', :action => 'new'

It's also not tolerant anymore to String / Symbol differences:


assert_redirected_to :controller => 'categories', :action => 'new'

won't match:


redirected_to :controller => 'categories', :action => :new

Comments and changes to this ticket

  • Jessy
  • Tom
  • josh

    josh December 3rd, 2008 @ 03:40 PM

    • State changed from “new” to “open”

    Sounds like a problem, got patch?

  • Tom

    Tom December 3rd, 2008 @ 04:11 PM

    I don't have a patch, but have a slew of tests that are affected by the new behavior of the assert_redirected_to. It is more than just being too strict, it appears to not match routes correctly from the routes.rb as well. I can post examples if it will help? I'd love to help if I can!

  • Tom

    Tom December 23rd, 2008 @ 10:24 PM

    Just trying to help:

    routes.rb:

    
    map.connect '/:address', :controller => 'page', :action => 'show'
    

    Unit Test:

    
    assert_generates  "/page-name", {:controller => "page", :action => "show", :address => 'page-name'}
    assert_recognizes "/page-name", {:controller => 'page', :action => 'show', :address => 'page-name'}
    

    This will not work in Rails 2.2.2

  • Tom

    Tom December 24th, 2008 @ 12:24 AM

    Okay, disregard my last post. This is a better way to capture the issue:

    routes.rb

    
     map.connect '/:address', :controller => 'page', :action => 'show'
    

    controller

    
     redirect_to('/page-name')
    

    test

    
    assert_redirected_to(ActionController::Routing::Routes.recognize_path(@response.redirected_to), @response.redirected_to)
    

    That test should always pass, since the recognize_path method will return the params hash of the redirected_to url. BUT

    
    Test::Unit::AssertionFailedError Exception: Expected response to be a redirect to <http://www.gracechurch.org/admin/display/show?address=page-name> but was a redirect to <http://www.gracechurch.org/page-name>.
    

    So, the assert_redirected_to is not assembling the correct route for the hash options provided in the first parameter.

  • Kieran P

    Kieran P February 16th, 2009 @ 08:27 AM

    • Tag changed from assertions, tests to 2.3, 2.3.0, assertions, tests

    +1

    Expected response to be a redirect to http://Kete/site but was a redirect to http://Kete/site/.

    I think the new functionality is better, but a trailing slash?!? Common!

  • James S

    James S February 23rd, 2009 @ 07:06 AM

    i have experienced a bunch of new failing tests also upon upgrading to 2.2.2 but it would appear that the issue isn't exactly a bug, but rather intentionally reduced functionality while increasing simplicity of the code for both the assert and redirect_to itself.

    i think this is the breaking commit: http://github.com/rails/rails/co...

    and this seems to be related: http://rails.lighthouseapp.com/p...

    my particular case is

    controller:

    
    redirect_to :controller => :patients, :action => :show, :id => @patient 
    

    test:

    
    assert_redirected_to :controller => :patients, :action => :show
    

    works, but if i change controller to

    
    redirect_to patient_path(@patient)
    

    i get

    
    response to be a redirect to <http://test.host/patients/996333082> but was a redirect to <http://test.host/patients/979761894>
    

    i didn't specify an ID in my assert at all - so something is wrong. this asserted fine in 2.1.

    the workaround for me is to use the patient_path approach in the controller and use

    
    assert_redirected_to assigns(:patient)
    

    in the test, as suggested by the maintainer in the second link posted above. which does work, and is actually cleaner in a way .. fine for my small site, but maybe this is less fine for others

  • Ryan Bigg

    Ryan Bigg April 11th, 2010 @ 10:55 PM

    • State changed from “open” to “incomplete”

    Using symbols / strings inconsistently is not quite a Rails bug. Nonetheless, if you wish to see this fixed, please submit a failing test case.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Rohit Arondekar

    Rohit Arondekar June 23rd, 2010 @ 03:01 AM

    Any updates to this ticket? This is still an issue on Rails master?

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • State changed from “incomplete” to “open”
    • Tag changed from 2.3, 2.3.0, assertions, tests to 23, 230, assertions, tests
    • Importance changed from “” to “”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • State changed from “open” to “stale”

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>

Pages