Skip to content

Enhancement: customize panic messages for expectation failures #658

Description

@BatmanAoD

It would be nice to be able to customize the panic messages for unmet expectations or even for calls without a matching expectation.

For unmet expectations, this should be easy - it would just be a method on the Expectation struct itself, something like .error_when_unmet(impl Fn() -> String).

For calls without a matching expectations, a particularly useful case is when there's a Predicate involved that has failed. Possibly there could be an implementation of Predicate that permits registering a callback to generate a custom message whenever the Predicate is checked and doesn't match, and then, when handling a method call, these messages could be collected and, if no match is found, listed in the panic message.

To clarify what this would look like to use, with the Foo I think something like this:

mock_foo
    .expect_foo()
    .once()
    .withf_message(|a, b| *a > 15, |a, b| format!("expected 'a' to be greater than 15 but got {a}"))
    .returning(|_, _| 42);
mock_foo.foo(1, 5);
mock_foo.checkpoint();

The panic would look something like this:

MockFoo::foo(1, 5): No matching expectation found
    Tried `MockFoo::foo: Expecation(<function>)`; unmet predicate: expected 'a' to be greater than 15 but got 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions