我正在创建一个单元测试,想要测试响应中返回的 JSON
结构。我知道 TestResponse
提供了一个 assertJsonStructure
方法来匹配你的 JSON
响应的结构。但由于某些原因,我无法将 $structure
映射到我的响应中,结果测试失败。让我分享所需的代码片段。
端点响应
{
"status": true,
"message": "",
"data": [
{
"id": 2,
"name": "Shanelle Goodwin",
"email": "chaz43@example.net",
"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
数组中可能有多个节点,所以我尝试在结构中提到该数组,但似乎映射不正确。任何帮助将不胜感激:-)
['data' => ['*' => [name]]]
。这是一个错误,而不是失败,并且它说:PHPUnit\Framework\Assert::assertArrayHasKey()的第二个参数(无值)必须是数组或ArrayAccess
。添加另一个属性后,错误消失了! - Khalil Laleh