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.

assert_redirected_to is too strict

#1490

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

Reported by Ivan · November 28th, 2008 @ 12:48 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Jessy
  2. Tom
  3. josh
    josh
    • State changed from new to open

    Sounds like a problem, got patch?

    December 3rd, 2008 @ 03:40 PM

  4. Tom
    Tom

    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!

    December 3rd, 2008 @ 04:11 PM

  5. Tom
    Tom

    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

    December 23rd, 2008 @ 10:24 PM

  6. Tom
    Tom

    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.

    December 24th, 2008 @ 12:24 AM

  7. Kieran P
    Kieran P
    • 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!

    February 16th, 2009 @ 08:27 AM

  8. James S
    James S

    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

    February 23rd, 2009 @ 07:06 AM

  9. Ryan Bigg
    Ryan Bigg
    • 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.

    April 11th, 2010 @ 10:55 PM

  10. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  11. Rohit Arondekar
    Rohit Arondekar

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

    June 23rd, 2010 @ 03:01 AM

  12. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 05:01 PM

  13. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:01 PM