shihabudheen's avatar

Laravel unit test for nested item in response.

How can I check value of API response in laravel unit test.

I want to check current_page equal to one from following response.

{ "data": [ [] ], "meta": { "current_page": 1, "from": 1, "last_page": 1, "path": "http://abc.local/api/v1/languages", "per_page": 15, "to": 4, "total": 4 } }

Here, I want to check current page from meta data, i can check json structure but no idea about comparing value.

0 likes
1 reply
mstrauss's avatar

How about this?

/** @test */
function test_json_response()
{
    $response = $this->json('GET', '/api/languages');

    $response
        ->assertStatus(200)
        ->assertJsonFragment([
            'per_page' => '15',
        ]);
}

Check out the Laravel News article on assertJsonFragment: https://laravel-news.com/testing-partial-json

Please or to participate in this conversation.