Skip to main content

assert_err_matches

Macro assert_err_matches 

Source
macro_rules! assert_err_matches {
    ($expr:expr, $variant:pat) => { ... };
    ($expr:expr, $variant:pat if $guard:expr) => { ... };
}
Expand description

Assert that an error matches a pattern, with an optional guard expression.

Prefer this over error_contains or substring checks — guards can inspect the inner value without forcing a string round-trip.

§Example

// Match variant only
assert_err_matches!(result, ForgeError::NotFound(_));

// Match variant with a guard on the inner value
assert_err_matches!(result, ForgeError::Validation(msg) if msg.contains("email"));

// Bind the inner value in the guard
assert_err_matches!(result, ForgeError::InvalidArgument(msg) if msg == "bad input");