I have a laravel pest test that checks a redirect after data is submitted to a controller to create a client.
it('redirects to the clients show page', function () {
$user = User::factory()->asAdmin()->create();
auth()->login($user);
post(route('clients.store'), [
'name' => 'Test Client',
'description' => 'Test Description',
'poc' => 'Test POC',
'poc_email' => 'Test POC Email',
'image_path' => 'Test Image Path',
])->assertRedirect(route('clients.show', Client::first()));
});
In isolation mode (only this test is executed) it works without a problem, but once i run the whole test suite with multiple test before this one (creating and removing clients), the test fails because the auto increment is not reset after each test resulting in this error message:
Failed asserting that two strings are equal.
Expected :'http://localhost/clients/4'
Actual :'http://localhost/clients/1'
<Click to see difference>
at vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:311
at vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:178
at tests/Feature/Controllers/ClientController/StoreTest.php:87
at vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:177
I'm using the refresh database trait for my pest test and I know that this behaviour is expected since for performance reasons the auto increment indicies are not reset after each test.
Here are some more information about my setup: DB: MariaDB 11.2 PHP: 8.3.1 Laravel: 10.40
The interesting part is that the route with the freshly created client model redirects to the correct id of '1' but the Client::first()
returns a client with a id of '4'.
Also this testsuite works with sqlite without any problems!
So my question is, how do I get a test that tests that same but does not rely on the inconsistent id of the client in this case?
Thanks for your help!
I found the solution myself. I was returning the result of Client::make([...])->save() as the route parameter, but the result of the save is the result of the database action, in this case a boolean true
, so it was casted to 1
and resulted in this error. After fixing this the test now runs without a problem!