Skip to main content

Module assertions

Module assertions 

Source
Expand description

Terse assertions for DomainError values.

Error tests otherwise repeat assert_eq!(err.category(), ...) and assert_eq!(err.code().as_str(), ...). These helpers — both free functions and a DomainErrorExt extension trait — make the intent obvious and the panic messages descriptive.

use klauthed_testing::assertions::{assert_category, assert_code, DomainErrorExt};
use klauthed_error::{DomainError, ErrorCategory, ErrorCode};

#[derive(Debug)]
struct NotThere;
impl std::fmt::Display for NotThere {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("missing")
    }
}
impl std::error::Error for NotThere {}
impl DomainError for NotThere {
    fn category(&self) -> ErrorCategory { ErrorCategory::NotFound }
    fn code(&self) -> ErrorCode { ErrorCode::new("thing.not_found") }
}

let err = NotThere;
// Free functions:
assert_category(&err, ErrorCategory::NotFound);
assert_code(&err, "thing.not_found");
// Or the fluent extension trait:
err.assert_category(ErrorCategory::NotFound)
   .assert_code("thing.not_found")
   .assert_http_status(404);

Traits§

DomainErrorExt
Fluent assertions on any DomainError, each returning &self so they chain.

Functions§

assert_category
Assert that err’s category equals expected.
assert_code
Assert that err’s code string equals expected.
assert_http_status
Assert that err’s http_status equals expected.
assert_retryable
Assert that err’s is_retryable equals expected.