#mocking #function #mockable #macro #foo #return-at

macro mockimbap

Mockimbap is a macro for mocking Rust functions

1 unstable release

0.1.0 Nov 25, 2024

#475 in Procedural macros

Download history 138/week @ 2024-11-23 13/week @ 2024-11-30 4/week @ 2024-12-07

155 downloads per month

MIT license

8KB
91 lines

🍙 Mockimbap

Mockimbap is a macro for mocking Rust functions

Crates.io Rust Version

Usage

Add the following to your Cargo.toml:

[dependencies]
mockimbap = "0.1.0"

Example

#[mockimbap::mockable]
trait Foo {
    fn foo(&self) -> i32;
}

#[return_at(foo, 1)]
#[mock(Foo)]
struct MockFoo;

❗️ Limitation

Currently, the macro only supports mocking functions that return a value. The macro does not support mocking functions that take arguments.

And Always must mock after mockimbap::mockable

So, you may do like this:

// mock.rs
#[mock(Foo)]
struct MockFoo;

// main.rs
#[mockimbap::mockable]
trait Foo {
    fn foo(&self) -> i32;
}

fn main() {
    let mock = MockFoo;
    assert_eq!(mock.foo(), 1);
}

// Like this, you must put mock file after mockimbap::mockable
mod mock;

Dependencies

~225–670KB
~16K SLoC