Laravel - HTTP Tests - Scoping Json Collection Assertions
Sometimes, your application's routes will return JSON collections that are assigned named keys:
Route::get('/users', function () {
return [
'meta' => [...],
'users' => User::all(),
];
})
When testing these routes, you may use the has
method to assert against the number of items in the collection. In addition, you may use the has
method to scope a chain of assertions:
$response
->assertJson(fn (AssertableJson $json) =>
$json->has('meta')
->has('users', 3)
->has('users.0', fn ($json) =>
$json->where('id', 1)
->where('name', 'Victoria Faith')
->missing('password')
->etc()
)
);
However, instead of making two separate calls to the has
method to assert against the users
collection, you may make a single call which provides a closure as its third parameter. When doing so, the closure will automatically be invoked and scoped to the first item in the collection:
$response
->assertJson(fn (AssertableJson $json) =>
$json->has('meta')
->has('users', 3, fn ($json) =>
$json->where('id', 1)
->where('name', 'Victoria Faith')
->missing('password')
->etc()
)
);