Daniel Pietzsch Daniel Pietzsch - 4 months ago 12
Ruby Question

How can I test only a part of the URL I am redirected to (using assert_redirected_to)?

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:


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:

I usually use
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

*(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).