In a functional test of my Rails app, I want to test where I am redirected to. The expected URL points to an external resource (that means it's not part of my application).
The URL looks like this: https://my.url.com/foo?bar1=xyz&bar2=123
Unfortunately I can't predict the parameters, because they are generated by an external resource.*
However, the rest of the URL always stays the same: https://my.url.com/foo
I usually use assert_redirected_to
for this kind of test, but this expects the whole URL, including the parameters.
Can anyone think of another way to test that redirection but only check for the first part of the URL without the parameters?
(the URL is not in the assigns
Hash)
*(I make an API call to an application, which responses with the URL I shall redirect_to)
The http request commands like post
, get
etc. create an instance variable called @response
when they are called*. @response
itself contains a method called redirect_url
which stores the URL you have been redirected to (in case you have really been redirected).
Therefor, I can just use a normal assert_match
to compare a regular expression to @response.redirect_url
:
post :my_action_to_test assert_response :redirect assert_match /https:\/\/my.url.com\/foo/, @response.redirect_url
*(actually, these http methods just use the private method process
, which creates the @response
variable).
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