Testing
A fresh scaffold passes make test, make lint and make format, and it is not because it has
few tests. Every generated piece comes with the tests that pin down its behaviour, in the crate
where that behaviour lives.
| Layer | Tests | Against |
|---|---|---|
| Domain model | the rules in new, value object parsing |
nothing |
| Errors | each From<ErrorRepositorio> arm |
nothing |
| Use cases | success and each failure path | a repository double or UnitOfWorkFactoryGrabadora |
| Repositories | persist, find, page, soft delete, uniqueness | in memory, or a real Postgres with #[sqlx::test] |
| Routes | status per outcome, over the real OpenApiService |
use case doubles |
| Error mappers | one test per variant: its status and its code | nothing |
| Consumers | ack, ack twice (idempotency), bad payload | envelopes built from an Object Mother |
| every template renders | nothing | |
| Screens | list, detail, form, errors under fields | in-memory fake repository |
| Spec | docs/api/openapi.json equals the live spec |
use case doubles |
Conventions
Section titled “Conventions”Puerto’s tests, and the ones its AGENTS.md asks you and your agent to write, follow one shape:
#[tokio::test]async fn deberia_rechazar_el_precio_cuando_no_es_positivo() { // Preparar // Actuar // Comprobar}- Names say
deberia_<expected_behaviour>_cuando_<scenario>(should … when …). // Preparar,// Actuar,// Comprobar(arrange, act, assert), in that order.- Tests live in the file they test, in a
#[cfg(test)] mod tests. - TDD: the failing test first, then the least code that passes it.
- No
unwrap()orexpect()outside tests.
Doubles come with the ports
Section titled “Doubles come with the ports”Every port is a trait, and every trait file carries its mockall double in a dobles module
behind #[cfg(any(test, feature = "pruebas"))]:
dominio_producto::repositorio::dobles::MockRepositorioProducto,
aplicacion_producto::crear_producto::dobles::MockCrearProducto,
dominio_correo::bienvenida::dobles::MockEnviadorCorreoBienvenida. A crate that uses another’s
doubles turns the pruebas feature on in its [dev-dependencies], so rest_producto’s tests use
aplicacion_producto’s doubles without shipping them in the binary.
let mut repositorio_doble = MockRepositorioProducto::new();repositorio_doble.expect_buscar_por_id().returning(|_| Ok(None));repositorio_doble.expect_guardar().never();In an events project, write use cases are tested with UnitOfWorkFactoryGrabadora
(dominio_producto::unit_of_work::dobles), which keeps what was saved, which events were
recorded and whether it committed:
let grabado = uow_factory.grabado();assert!(grabado.confirmado);assert_eq!(grabado.guardados.len(), 1);Object Mothers
Section titled “Object Mothers”contextos/producto/dominio/src/pruebas/madres.rs builds valid entities, so a test states only
what matters to it:
let producto = MadreProducto::aleatorio();let barato = MadreProducto::new().con_precio(1).construir();let varios = MadreProducto::aleatorios(5);let props = MadreProducto::new().con_nombre_vacio().construir_props();Every field has .con_<campo>(valor), and required String fields also .con_<campo>_vacio().
Mothers are behind the same pruebas feature as the doubles
(dominio_producto::pruebas::madres::MadreProducto), so route tests and worker tests build return
values with them instead of a second set of fixtures. The frontend has its own
(web/src/test-support/producto.ts), next to an in-memory fake of the repository.
Running them
Section titled “Running them”make test # cargo nextest on the workspace, then bun test in web/make test/dominio # every dominio_* cratemake test/aplicacion # every aplicacion_* cratemake test/infraestructura # every infra_* crate; needs the database in --db projectsmake lint # clippy -D warnings, plus typecheck, Biome and the dependency rule in web/make format # cargo fmt --checkOne crate is cargo test -p dominio_producto.
In a --db project, repository tests run against a real database, so start it first with
make docker-compose/up. Each one is a #[sqlx::test(migrator = "migrador::MIGRADOR")]: a
throwaway database per test, migrated by the project’s one migrator, so no test carries its own
copy of the migrations. The rows a test needs come from the crate’s pruebas::siembra::sembrar.