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?
This intended.
What you want is something like this: https://github.com/BootstrapCMS/CMS/blob/master/tests/BasicTest.php#L33.
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.
Most helpful comment
What you want is something like this: https://github.com/BootstrapCMS/CMS/blob/master/tests/BasicTest.php#L33.