Laravel 13.34 New Published on

Counting the rows of several tables in one assertion

assertDatabaseCount() accepts an array: the expected number of rows in each table, checked in a single assertion.

Topics covered: TestingDatabase

New in Laravel 13.34. To check that an action leaves the right number of rows in several tables, you used to chain assertDatabaseCount() calls, one per table. The assertion now accepts an array.

Before

$this->assertDatabaseCount('reservations', 1);
$this->assertDatabaseCount('event_log', 1);
$this->assertDatabaseCount('waiting_list', 0);

Since Laravel 13.34

$this->assertDatabaseCount([
    'reservations' => 1,
    'event_log' => 1,
    'waiting_list' => 0,
]);

Each table is counted in turn, and the failure message names the one that doesn't add up. The keys can also be model classes.

Your task

POST /api/tables/{table}/reservations books a seat at a shared table of four, and writes the event to the log (the event_log table). When the table is full, the guest goes on the waiting list (the waiting_list table). The tests in tests/Feature/ReservationTest.php check that the rows exist, not how many there are: a booking saved twice would go unnoticed. Strengthen them with assertDatabaseCount([…]). Every mutant must make your tests fail.

Where the feature comes from: the original pull request.