Laravel – Testing what happens after a redirect


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

return Redirect::route('home')->with("message", "Ok!");

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:

public function testMessageSucceeds() {
    $crawler = $this->client->request('POST', '/contact', ['email' => '[email protected]', 'message' => "lorem ipsum"]);


    $message = $crawler->filter('.success-message');

    // Here it fails
    $this->assertCount(1, $message);

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

Session::flash('message', 'Ok!');
return $this->makeView('staticPages.home');

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


You can get PHPUnit to follow redirects with:

Laravel >= 5.5.19:


Laravel < 5.4.12:



$response = $this->followingRedirects()
    ->post('/login', ['email' => '[email protected]'])

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

For versions between these two:

See for a workaround.

Answered By – huntie

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published