首页 > 解决方案 > Laravel 功能测试,但数据直到请求后才会出现

问题描述

我遇到了功能测试和数据似乎既存在又不存在的问题,具体取决于从何处以及何时检索该数据。

我的测试似乎只有在我一次运行一个时才有效,但如果将它们一起运行,php artisan test那么似乎没有数据,所以测试失败

这是我的课:

<?php


namespace Tests\Feature;

use App\AssetGroup;
use Throwable;
use Illuminate\Foundation\Testing\RefreshDatabase;

class AssetGroupTest extends CoreFeatureTest
{
    use RefreshDatabase;

    public function setUp(): void
    {
        parent::setUp();
        $this->seed();
        $this->logMeIn();
    }
    
        public function testAssetGroupShow(): void
    {
        $response = $this->get('/api/asset-group?keyBy=none');

        $response->assertStatus(200);

    }

    public function testAssetGroupDelete(): void
    {
        $assetGroup = AssetGroup::whereHas('user', function ($query) {
            $query->where('user_id', 1);
        })->first();

        $response = $this->delete('/api/asset-group/' . $assetGroup->uuid);

        $response->assertStatus(200);
        $this->assertSoftDeleted($assetGroup);
    }
}

标签: phplaravelphpunit

解决方案


推荐阅读