Framework: [5.1.8] Testing redirections, never asserted because of followRedirects()

Created on 24 Jul 2015  路  5Comments  路  Source: laravel/framework

If someone visits my root URL unauthenticated, he will be redirected to a login page. The following test passes successfully:

$response = $this->call('GET', '/');
$this->assertEquals(302, $response->getStatusCode());
$this->assertInstanceOf('Illuminate\Http\RedirectResponse', $response);
$this->assertEquals($this->baseUrl . '/auth/login', $response->getTargetUrl());

But now this (which I would expect to be semantically the same) fails:

$this->visit('/')
     ->assertRedirectedTo($this->baseUrl . '/auth/login');

with the following message: Failed asserting that Illuminate\Http\Response Object (...) is an instance of class "Illuminate\Http\RedirectResponse".

That's because CrawlerTrait::makeRequest calling followRedirects makes sure in the test case we always end up with the final response after redirection.

I assume this automatic redirection-following of makeRequest is intentional behavior, and in most cases appropriate, although an easy way to disable it would be nice. But I wonder, is this intentional behavior of TestCase::assertRedirectedTo? Does it ever pass, if calling it is only possible after all redirection is done?

Most helpful comment

All 5 comments

This intended.

That tests that when i hit the root url, i get redirected to another page.

@GrahamCampbell , what is the reason that this is an intended behavior ?

@davo3 when using ->visit as it automatically follows redirections you wont be able to test them. If you want to test with ->assertRedirectedTo, use ->get instead.

Was this page helpful?
0 / 5 - 0 ratings