단위 테스트를 만들고 있는데 응답에서 반환 된 JSON
구조체를 테스트하려고합니다. TestResponse
은 JSON
응답 구조와 일치하도록 assertJsonStructure
메서드를 제공합니다. 그러나 어떤 이유로 나는 $structure
을 내 응답에 매핑 할 수 없으므로 테스트가 실패합니다. 필요한 스 니펫을 공유하겠습니다.PhpUnit 테스트에서 일치하는 JsonStructure - Laravel 5.4
엔드 포인트 응답
{
"status": true,
"message": "",
"data": [
{
"id": 2,
"name": "Shanelle Goodwin",
"email": "[email protected]",
"created_at": "2017-03-05 16:12:49",
"updated_at": "2017-03-05 16:12:49",
"user_id": 1
}
]
}
테스트 기능
public function testEndpoint(){
$response = $this->get('/api/manufacturer/read', [], $this->headers);
$response->assertStatus(200);
$response->assertJsonStructure([
'status',
'message',
'data' => [
{
'id',
'name',
'email',
'created_at',
'updated_at',
'user_id'
}
]
]);
var_dump("'/api/manufacturer/read' => Test Endpoint");
}
이 할 수 data
배열의 여러 노드 내가 구조의 배열을 언급하려하지만 '아무튼 보인다 이유가 그래서 어떤 도움을 주시면 감사하겠습니다 :-)
매우 유용합니다! 공식 문서에 있어야합니다. 우리를 위해 낚시 해 줘서 고마워. –
내 영웅, 감사합니다 –
[코드가 옮겼습니다] (https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Testing/TestResponse.php#L363) –