Assert specific error in tests

From 0xMiden/protocol · View interactive version

Assert specific error in tests is an AI agent skill from the GitHub repository 0xMiden/protocol. Use when writing a Rust test that exercises a failure path or a MASM test that expects a `panic` / `assert` — assert on the specific expected error variant or error code.

How to use this skill

Skill file

Negative Tests Must Pin the Expected Error

Rule

A test that exercises an error path must assert on the specific error returned:

If multiple error conditions could plausibly fire on the same input, assert on the one this test is actually exercising.

Why

A test that only checks is_err() passes even when an unrelated bug breaks the function, so it no longer validates the failure mode it claims to. Pinning the exact variant also catches reorderings where one error path starts firing instead of another.

Examples

// Good
use assert_matches::assert_matches;

let result = AccountId::try_from(&bytes);
assert_matches!(result, Err(AccountError::InvalidLength { expected: 32, got: 5 }));

// Bad
let result = AccountId::try_from(&bytes);
assert!(result.is_err());
// Good (MASM test)
let err = run_kernel(...).unwrap_err();
assert_eq!(err.code(), ERR_NOTE_NOT_FOUND);

// Bad
assert!(run_kernel(...).is_err());
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.