Got it:
->assertJsonStructure([
'data' => [
'body',
'title',
'active'
]
])
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Per the Laravel 5.4 documentation:
$response->assertJsonStructure(array $structure);
...should "assert that the response has a given JSON structure." (documentation ends there)
How would I make the array $structure to represent this json for testing:
{
"data": {
"title": "Nam saepe earum molestias consequuntur et doloremque ea.",
"body": "Consequatur iure omnis distinctio tempore accusamus...",
"active": false
}
}
The context of my question is following Laracast Incremental APIs: Level 7: Tests...Readable Ones! Here is the excerpt of my test where I need help filling in the ??:
/** @test **/
public function it_fetches_a_single_lesson()
{
// arrange
$this->makeLesson([
'title' => 'pets',
'body' => 'Millie, Kitcha, Jo Jo, Bill',
'some_bool' => true
]);
// act
$response = $this->json('GET', '/api/v1/lessons/1');
// assert
$response ->assertStatus(200)
->assertJsonStructure([
// ??
]);
}
Got it:
->assertJsonStructure([
'data' => [
'body',
'title',
'active'
]
])
Please or to participate in this conversation.