我用 Pest 为我的 Laravel 应用程序编写了大约 90 个测试。大多数时候,所有测试都会通过,但有时,即使我不对代码库进行任何更改,我也会收到错误。
错误是相同的,但每次不同的测试都会发生。失败的测试唯一的共同点是使用
seed()
函数。
每次使用
php artisan db:seed
和 php artisan migrate:fresh -- seed
时数据库种子都会正确
• Tests\Feature\Domain\Product\Resources\DeleteProductTest > it can remove a product
InvalidArgumentException
You requested 1 items, but there are only 0 items available.
at tests/Feature/Domain/Product/Resources/DeleteProductTest.php:17
13▕ /** @var \Domain\User\Models\User $bob */
14▕ $bob = User::factory()->create();
15▕ actingAs($bob);
16▕
➜ 17▕ seed();
18▕
19▕ $product = Product::all()->random();
20▕
我正在使用 RefreshDatabase 特征,并且在运行测试批次之前清除缓存。
有谁知道为什么会发生这种情况或如何解决它?
我自己修好了。有些逻辑依赖于随机性,有时数据库没有正确播种。