stwilson
2 years ago
1432
3
Testing

Testing JSON APIs, specifically: assertJsonStructure

Posted 2 years ago by stwilson

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([
                    // ??
                ]);
}

Please sign in or create an account to participate in this conversation.