Attribute Macro unimock::unimock

source · []
#[unimock]
Expand description

Autogenerate mocks for all methods in the annotated traits, and impl it for Unimock.

Mock generation happens by declaring a new MockFn-implementing struct for each method.

Example

use unimock::*;

#[unimock]
trait Trait1 {
    fn a(&self) -> i32;
    fn b(&self) -> i32;
}

#[unimock]
trait Trait2 {
    fn c(&self) -> i32;
}

fn sum(obj: impl Trait1 + Trait2) -> i32 {
    obj.a() + obj.b() + obj.c()
}

fn test() {
    // Unimock now implements both traits:
    sum(mock(None)); // note: panics at runtime!

    // Mock a single method (still panics, because all 3 must be mocked:):
    sum(mock(Some(Trait1__a.next_call(|_| true).returns(0).once().in_order())));
}

Arguments

The unimock macro accepts a number of comma-separated key-value configuration parameters:

  • #[unimock(mod=ident)]: Puts the MockFn types in a new module named ident.
  • #[unimock(as=[a, b, c])]: Given there are e.g. 3 methods in the annotated trait, assigns the names a, b and c for the MockFn types respectively, in the same order as the trait methods.
  • #[unimock(unmock=[a, b, _]): Given there are e.g. 3 methods in the annotated trait, uses the given paths as unmock implementations. The functions are assigned to the methods in the same order as the methods are listed in the trait. A value of _ means no unmock support for that method. See Unmock for more information.
  • #[unimock(prefix=path)]: Makes unimock use a different path prefix than ::unimock, in case the crate has been re-exported through another crate.