Match JsonStructure in PhpUnit Test - Laravel 5.4
I think you should use:
$response->assertJsonStructure([
'status',
'message',
'data' => [
[ // change here
'id',
'name',
'email',
'created_at',
'updated_at',
'user_id'
] // change here
]
]);
Luckily, playing with different options I have solved this issue. A '*' is expected as key if we are to match a nested object in an array. We can see the reference here.
Source: TestResponse
I have set the structure like this for array of
objects`
$response->assertJsonStructure([
'status',
'message',
'data' => [
'*' => [
'id',
'name',
'email',
'created_at',
'updated_at',
'user_id'
]
]
]);
And if you want to match just a single object
$response->assertJsonStructure([
'status',
'message',
'data' => [
[
'id',
'name',
'email',
'created_at',
'updated_at',
'user_id'
]
]
]);