Friday, October 10, 2025
HomePHPAsserting a JSON Response Construction in Laravel

Asserting a JSON Response Construction in Laravel


When writing checks for API responses in Laravel, it may be helpful to validate the construction of the response. Laravel has the fluent assertJson() technique, which you should use to confirm JSON values in a given check response:

it('Returns Arizona sports activities groups', perform () {

$this->get('api/groups/arizona')

->assertJson(perform (AssertableJson $json) {

$json->has('groups', 3);

$json->has('groups.0', perform (AssertableJson $json) {

$json

->the place('title', 'Phoenix Suns')

->and so on();

});

});

});

Given the above check, here is a static instance of the JSON information:

{

"groups": [

{

"name": "Phoenix Suns",

"sport": "Basketball"

},

{

"name": "Arizona Cardinals",

"sport": "Football"

},

{

"name": "Arizona Diamondbacks",

"sport": "Baseball"

}

]

}

Our check validates that three groups are listed in Arizona and that the title property exists on the primary document. That is glorious for validating the precise values to pattern the response utilizing the highly effective JSON assertion APIs in Laravel. To enrich these assertions, we will additionally validate the final construction of the entire response:

$response->assertJsonStructure([

'teams' => [

'*' => ['name', 'sport'],

],

]);

One caveat to the assertJsonStucture() assertion: if we add a brand new key sooner or later, this check will proceed to go. When you require extra exactness, you would possibly want to succeed in for the assertExactJson(). For simply generalizing a JSON construction to make sure particular properties exist within the response, assertJsonStructure() can provide you confidence that your complete construction comprises properties you anticipate.

When you want extra in depth assertions across the construction of the JSON, you may also wish to attain for whereType() and whereAllType() assertions. Given our earlier instance, you might validate the categories in your JSON responses utilizing the next:

$response->assertJson(perform (AssertableJson $json) {

$json->has('groups', 3);

$json->has('groups.0', perform (AssertableJson $json) {

$json->whereAllType([

'name' => 'string',

'sport' => 'string',

]);

});

});

Utilizing whereAllType requires us to outline varieties for each key within the groups merchandise, until you employ the above with ->and so on():

$json->whereAllType([

'name' => 'string',

// 'sport' => 'string',

])->and so on();

As talked about, the above code does not assert the entire response and assumes the opposite groups have the identical construction. You would assert every staff within the array, and even use Eloquent manufacturing facility information to validate the response values match. Usually, combining the above assertions will guarantee you’ve gotten the anticipated JSON response form, and you’ll combine in additional sophisticated assertions the place wanted. See the Laravel Documentation for extra examples and helpful JSON assertions.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments