我创建了一个功能测试 api。
CategoryApiTest.php
namespace Tests\Feature;
use App\Models\Category;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\JsonResponse;
use Tests\TestCase;
class CategoryApiTest extends TestCase
{
use RefreshDatabase;
protected $faker;
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->user = $this->faker = Category::factory()
->create([
'name' => 'Food',
'description' => 'This is the food category',
'menu' => true,
'image' => 'IMG_4985',
'parent_id' => '1'
]);
}
...
public function testUpdateCategory()
{
$response = $this->putJson('/api/categories/1', [
'name' => 'Water Update',
'description' => 'This is the water category update',
'menu' => true,
'image' => 'IMG_56898',
'parent_id' => '1'
]);
$response->assertStatus(JsonResponse::HTTP_OK)
->assertJsonFragment([
'name' => 'Water Update',
'slug' => 'water-update',
'parent_id' => 1
]);
}
...
}
这种情况总是返回失败,因为找不到带有 的类别id = 1
。
我检查了列表类别,我看到:
我创建了文件.env.testing
和数据库测试,但我认为phpunit.xml
不使用文件.env.testing
所以RefreshDatabase
不起作用。
数据测试没有refresh
。有问题,请帮我解决。