Asserting failures
Testing that a mistake is refused: wiring, connect, load and lookup errors read as values.
Ulo refuses most mistakes at startup, and every startup error is a value with fields. A test asserts the key, the module and the reader and does not match on text. The error types in the listings (StartupError, WiringError, ConnectError, LoadError, LoadRefusal, LookupError, ShutdownFailure, ServeEnd) are all exported from ulo.
A wiring refusal needs no runtime
wire() creates no instance and performs no I/O. A plain #[test] calls it, with no async runtime and no .runtime(..). This is the pattern in examples/06-modules:
#[controller]
struct Snooper {
_hasher: Dep<PasswordHasher>,
}
#[handlers]
impl Snooper {
#[ulo_http::get("/")]
fn snoop(&self) -> &'static str {
"never"
}
}
#[module(imports = [UsersModule], controllers = [Snooper])]
struct SnoopingModule;
#[test]
fn reading_a_private_binding_fails_wiring_with_the_key_and_the_reader() {
let Err(StartupError::Wiring(errors)) = App::builder(SnoopingModule).wire() else {
panic!("wiring should fail");
};
let [WiringError::Missing { key, consumer, module, near }] = errors.iter().collect::<Vec<_>>()[..] else {
panic!("expected one missing dependency, got {errors}");
};
assert_eq!(key.to_string(), "PasswordHasher");
assert_eq!(consumer, "Snooper (field `_hasher`)");
assert_eq!(module.to_string(), "SnoopingModule");
assert!(near.is_none());
}Three practices for these tests:
- Destructure the whole slice,
let [WiringError::Missing { .. }] = ... If a second, unrelated error appears, the pattern stops matching and the test shows what else was reported. - Compare
key.to_string()andmodule.to_string(). They print the short names a report prints. - Pin the printed form once, with
errors.to_string().contains("missing dependencyPasswordHasher"), for the messages a person reads.
WiringError is #[non_exhaustive], so a match outside the crate needs a wildcard arm. A let ... else with one variant, as above, does not.
Check the test against a good input too. A refusal assertion that is never shown to pass on valid wiring cannot tell a working check from one that fires on everything. Examples 06 and 13 pair each refusal with a test of the working application.
Several problems at once
wire() returns all of them. examples/11-wiring-report pins five at once and checks the order the passes report them in:
let Err(StartupError::Wiring(errors)) = builder().wire() else {
panic!("the broken app should fail wiring");
};
assert_eq!(errors.len(), 5);
let all: Vec<&WiringError> = errors.iter().collect();
let [duplicate, missing, ambiguous, cycle, scope] = all[..] else { unreachable!() };Source locations are &'static Location values. The example checks that a duplicate's two locations are in src/lib.rs and one line apart.
A connect refusal
connect() is async and needs a runtime. Build the app, wire it, and match the error. This is the pattern in examples/02-providers for a failing constructor:
let StartupError::Connect(ConnectError::Construct { key, module, reason }) = &error else {
panic!("expected a construct failure, got {error}");
};
assert_eq!(key.to_string(), "Greeter");
assert_eq!(module.to_string(), "EmptySalutationModule");
assert_eq!(reason.to_string(), "the salutation is empty");reason is a FailureReason. examples/09-lifecycle matches FailureReason::Errored for a readiness check that never passes and asserts the attempt count: see Lifecycle hooks. That test runs on a paused clock so the backoff costs no real time.
A lazy load refusal
LoadError::Refused(LoadRefusal) from examples/08-dynamic-and-lazy:
let Err(LoadError::Refused(LoadRefusal::Controllers { module })) = server.handle.load(RoutesModule).await else {
panic!("the load should be refused");
};
assert_eq!(module.to_string(), "RoutesModule");A lookup that must miss
Before a lazy module loads, its provider is not visible:
assert!(matches!(server.handle.get::<Reports>().await, Err(LookupError::NotFound { .. })));LookupError is #[non_exhaustive] too.
A shutdown with a failure
serve and close return Result<Shutdown, ShutdownError>. examples/10-shutdown asserts that a dead transport fails the shutdown and names itself:
let [ShutdownFailure::Serve { transport, end: ServeEnd::Errored(reason) }] = &*error.failures else {
panic!("expected the one `Serve` failure: {error}");
};
assert_eq!(*transport, TypeName::of::<Doomed>());
assert_eq!(reason.to_string(), "the listener was lost");Compile-time refusals
Some mistakes are compile errors: a hook on an execution-scoped type, Ext<T> in an explicit singleton, a field that is not an injection point. The framework tests these with trybuild fixtures in crates/ulo-codegen-tests/tests/ui, each a small program with the .stderr it must produce. They are sensitive to the compiler version, so that crate pins one toolchain. An application rarely needs the same.