'How to properly test redirect and what happens after in Laravel?
I have simple route redirect to lang like this:
Route::get('/', function() {
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && in_array(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2), config('blog.locales'))) {
$locate = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
} else {
$locale = config('app.fallback_locale');
}
return redirect()->to($locale);
});
The problem occurs when I want to test it via PHPUnit like this:
public function testRedirectToLang()
{
$response = $this->get('/');
$response->assertStatus(302);
$response->assertRedirect('/en');
}
public function testBlogPostsFetch()
{
$posts = Models\Blog\Posts\Post::factory(10)->hasTranslations()->create();
$response = $this->followingRedirects()->get('/');
$response->assertStatus(200);
$response->assertSee('<article class="post-short flex flex-wrap">', FALSE);
}
If I run both tests the second one fails:
If I run only second test it passes:
So basically something happens after first test which produces 404 for second test.
In general route works live fine for a few years. Just now decided to write tests...
Added print of routes in to the tests and it seems to be empty after first test...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|




