Laravel Pest 测试忽略不重置 mariadb 自动增量 ids

问题描述 投票:0回答:1

我有一个 Laravel pest 测试,可以在数据提交到控制器以创建客户端后检查重定向。

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()));
});

在隔离模式下(仅执行此测试),它可以正常工作,但是一旦我在这之前运行整个测试套件并进行多个测试(创建和删除客户端),测试就会失败,因为每次之后都不会重置自动增量测试导致此错误消息:

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

我正在使用刷新数据库特征进行害虫测试,并且我知道这种行为是预期的,因为出于性能原因,每次测试后不会重置自动增量索引。

以下是有关我的设置的更多信息: 数据库:MariaDB 11.2 PHP:8.3.1 拉拉维尔:10.40

有趣的部分是,新创建的客户端模型的路由重定向到正确的 id“1”,但

Client::first()
返回 id 为“4”的客户端。

这个测试套件也可以与 sqlite 一起使用,没有任何问题!

所以我的问题是,在这种情况下,如何获得一个测试相同但不依赖于客户端不一致的 id 的测试?

感谢您的帮助!

laravel mariadb auto-increment pest
1个回答
0
投票

我自己找到了解决方案。我返回 Client::make([...])->save() 的结果作为路由参数,但保存的结果是数据库操作的结果,在本例中是布尔值

true
,所以它被转换为
1
并导致了这个错误。修复此问题后,测试现在运行没有问题!

© www.soinside.com 2019 - 2024. All rights reserved.