Laravel - HTTP Tests - Asserting Json Types
You may only want to assert that the properties in the JSON response are of a certain type. The Illuminate\Testing\Fluent\AssertableJson
class provides the whereType
and whereAllType
methods for doing just that:
$response->assertJson(fn (AssertableJson $json) =>
$json->whereType('id', 'integer')
->whereAllType([
'users.0.name' => 'string',
'meta' => 'array'
])
);
You may specify multiple types using the |
character, or passing an array of types as the second parameter to the whereType
method. The assertion will be successful if the response value is any of the listed types:
$response->assertJson(fn (AssertableJson $json) =>
$json->whereType('name', 'string|null')
->whereType('id', ['string', 'integer'])
);
The whereType
and whereAllType
methods recognize the following types: string
, integer
, double
, boolean
, array
, and null
.