Reputation: 9825
I want to write a test that verifies that a URL is redirecting to a different one.
For example:
http://localhost:3000/news/foobar+baz/bar+boo
to
http://localhost:3000/news?tools=foobar+baz&sources=bar+boo
Using RSpec I can't seem to find a way to do this. Any suggestions?
Upvotes: 1
Views: 1797
Reputation: 311
You should make a request test. RSpec have:
expect(response).to redirect_to(...)
but it doesn't make assertions for the params
You use this one:
def expect_to_redirect_to_with_params(expected_url, expected_params)
redirect_url = response.location
uri = URI.parse(redirect_uri)
redirect_params = Rack::Utils.parse_query(uri.query)
expect(redirect_url).to eq(expected_uri)
expect(redirect_params).to eq(expected_params)
end
If you want you can make it to a custom matcher instead of method.
Upvotes: 0
Reputation: 537
In your case, you actually are trying to redirect from /foobar+baz/bar+boo to / with parameters appended. Potentially similar with this post: RSpec testing redirect to URL with GET params
Typically you shouldn't test the parameter with the redirect url syntax cus this is not the url location but the parameter. You can extract the location manually and parse the parameter to see if they are what you want. The first answer in that post gives an example.
Upvotes: 1