Testing
Database Testing
Database assertions let you verify that your application correctly reads and writes to the database. They are available on every TestCase instance and query the real database.
Available assertions
Example
How withTransaction keeps the database clean
withTransaction wraps your callback in a Knex transaction and always rolls it back when the callback finishes — even if it throws. Your test data never survives to the next test.
The rollback is automatic and happens whether the test passes or throws. You do not need to clean up manually.
Under the hood the framework uses AsyncLocalStorage (Builder.txStorage) to propagate the Knex trx object to every database query made inside the callback — including queries inside your controllers and models — without you having to pass anything around.
How model assertions work
assertModelExists(model) and assertModelMissing(model) use the model's table name and primary key:
The model must have getTable() and getKeyName() / getKey() methods (provided by the JCC Eloquent base model).
Notes
- All assertions throw on failure with a descriptive message
assertDatabaseHasmatches rows where ALL provided columns match (an AND query)- Make sure your
.envtest database is configured before running tests; assertions run against the real database