【发布时间】:2021-06-08 14:14:42
【问题描述】:
我正在尝试使用我在 laravel 8 中创建的 RESTful API 编写功能测试。
我有ChatController 通过getUserChats 方法获取用户聊天:
// get chats for user
public function getUserChats()
{
$chats = $this->chats->getUserChats();
return ChatShowResource::collection($chats);
}
这条路线调用的:
Route::get('chats', [
App\Http\Controllers\Chats\ChatController::class, 'getUserChats'
]);
这是我在这条路线上的功能测试:
public function test_get_user_chats()
{
// creates the fake user
$user = User::factory()->create();
$this->actingAs($user, 'api');
// adds the chat to new fake user that created
$response_create_chat = $this->json('POST', '/api/chats', [
'recipient' => $user->username,
'body' => 'some chat text',
]
);
$response_create_chat
->assertStatus(201)
->assertJsonPath('data.body', 'some chat text');
$response_get_chats = $this->json('get', '/api/chats');
$response_get_chats->assertStatus(200);
}
此绿色输出OK (1 test, 3 assertions) 成功运行测试,但我不知道这是实现它的最佳方式。
所以问题是我在测试中还需要做什么?我在做正确的功能测试吗?
【问题讨论】:
标签: php laravel testing phpunit laravel-8