我正在創建一個單元測試,並且想要測試響應中返回的JSON
結構。我知道TestResponse
提供了一種方法assertJsonStructure
來匹配您的JSON
響應的結構。但由於某種原因,我無法將$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
陣列中的多個節點所以這就是爲什麼我想提結構數組,但似乎它不」 t地圖正確。任何幫助將不勝感激:-)
非常有用!應該在官方文檔中。感謝您爲我們釣魚。 –
我的英雄,謝謝 –
[代碼已移動](https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Testing/TestResponse.php#L363) –