MockMe

MockMe is a tool used to mock dependencies / function calls when running unit (lib) tests in Rust.

How to Use

Currently, only works on nightly

Simply use the macro as seen in the example below. When this code is run normally, MockMe will have no effect. However, when the code is run as part of a unit test #[cfg(test)], the mocked token will be used instead.

```rust

#![feature(proc_macro)] extern crate mockme; use mockme::{mock, inject};

// Below we will create two mocking identifiers called id1 and id2. // We will then provide the name of the two functions we are mocking, as well as // their type signature. In future iterations, hopefully the signature won't be needed.

[mock(id1="externaldbcall: fn(u32) -> String", id2="other_call: fn() -> String")]

fn mysupercoolfunction() -> String { let input = 42u32; // externaldbcall will be replaced with fakemockedcall during testing let dbresult = externaldbcall(input);

// other_call will also be replaced
let other_result = other_call();
format!("I have two results! {} and {}", db_result, other_result)

}

// Finally, when we run our tests, we simply need to provide the identifier we previously used, // as well as the name of the replacement function

[test]

[inject(id1="dbfake", id2="otherfake")]

fn actualtest2() { let result = mysupercoolfunction(); assert_eq!(result, "I have two results! Faker! and This is indeed a disturbing universe."); }

fn dbfake(: u32) -> String { "Faker!".tostring() } fn otherfake() -> String { "This is indeed a disturbing universe.".to_string() } ```

Contributions

All contributions are welcome! This library is still in its infancy, so everything helps. Code contributions, feature requests and bug reports are all appreciated.

Limitations

Currently, the library is unable to infer the signature of the function that is being mocked. As a result, the programmer needs to provide it, which hurts the ergonomics of the library.