Laravel - Testing what happens after a redirect Laravel - Testing what happens after a redirect laravel laravel

Laravel - Testing what happens after a redirect


You can get PHPUnit to follow redirects with:

Laravel >= 5.5.19:

$this->followingRedirects();

Laravel < 5.4.12:

$this->followRedirects();

Usage:

$response = $this->followingRedirects()    ->post('/login', ['email' => 'john@example.com'])    ->assertStatus(200);

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.


You can tell crawler to follow a redirect this way:

$crawler = $this->client->followRedirect();

so in your case that would be something like:

public function testMessageSucceeds() {    $this->client->request('POST', '/contact', ['email' => 'test@test.com', 'message' => "lorem ipsum"]);    $this->assertResponseStatus(302);    $this->assertRedirectedToRoute('home');    $crawler = $this->client->followRedirect();    $message = $crawler->filter('.success-message');    $this->assertCount(1, $message);}


Since Laravel 5.5 to test redirect you can use assertRedirect:

/** @test */public function store_creates_claim(){    $response = $this->post(route('claims.store'), [        'first_name' => 'Joe',    ]);    $response->assertRedirect(route('claims.index'));}