I wrote a small test to check redirecting and URL parameters. Strangely the test fails, even though the error message seems to indicate the result is actually correct:
Failure/Error: response.should redirect_to(movies_path(:sort => 'title'))
Expected response to be a redirect to
<http://test.host/movies?sort=title>
but was a redirect to
<http://test.host/movies?ratings%5BG%5D=G&ratings%5BNC-17%5D=NC-17&ratings%5BPG%5D=PG&ratings%5BPG-13%5D=PG-13&ratings%5BR%5D=R&sort=title>
The URLs are identical (as they should be) and the expected parameter 'sort=title' is included in the parameters of the actual result. I believe this is a valid situation...
Acoording to http://api.rubyonrails.org/classes/ActionDispatch/Assertions/ResponseAssertions.html#method-i-assert_redirected_to: "This match can be partial, such that assert_redirected_to(controller: "weblog") will also match the redirection of redirect_to(controller: "weblog", action: "show") and so on."
SOLUTION:
I was not able to get the proposal of the selected answer to work, but the reply was useful in explaining that the 'partial' match in the above link is somewhat misleading, and the code did not work as expected. So, I created a Hash
with all the parameters received, and added them to the 'should redirect_to' test:
response.should redirect_to(movies_path(:sort => 'title', :ratings => rest))
where 'rest' is that hash.
The documentation is misleading. Internally, assert_redirected_to
calls normalize_argument_to_redirection
, which (when a hash is given as the argument) calls url_for
before performing the comparison. Basically, a partial match will work only if the route has the default pattern :controller/:action/:id
, and your asserted path is a hash with the controller or controller and action as keys.
You should put something like:
response.should redirect_to(movies_path(:sort => 'title', :ratings => "Ratings"))
It should be a complete path for the redirect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With