Skip to content
Advertisement

Laravel – Testing what happens after a redirect

I have a controller that after submitting a email, performs a redirect to the home, like this:

JavaScript

I am writing the tests for it, and I am not sure how to make phpunit to follow the redirect, to test the success message:

JavaScript

If I substitute the code on the controller for this, and I remove the first 2 asserts, it works

JavaScript

But I would like to use the Redirect::route. Is there a way to make PHPUnit to follow the redirect?

Advertisement

Answer

You can get PHPUnit to follow redirects with:

Laravel >= 5.5.19:

JavaScript

Laravel < 5.4.12:

JavaScript

Usage:

JavaScript

Note: This needs to be set explicitly for each request.


For versions between these two:

See https://github.com/laravel/framework/issues/18016#issuecomment-322401713 for a workaround.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement