2 unstable releases

0.2.0 Dec 22, 2020
0.1.0 Sep 7, 2020

#12 in #mocking

Download history 380/week @ 2021-02-26 723/week @ 2021-03-05 800/week @ 2021-03-12 694/week @ 2021-03-19 998/week @ 2021-03-26 1028/week @ 2021-04-02 1028/week @ 2021-04-09 1296/week @ 2021-04-16 711/week @ 2021-04-23 1128/week @ 2021-04-30 1090/week @ 2021-05-07 1239/week @ 2021-05-14 1068/week @ 2021-05-21 1358/week @ 2021-05-28 868/week @ 2021-06-04 679/week @ 2021-06-11

3,639 downloads per month
Used in 6 crates (4 directly)

MIT/Apache

12KB
178 lines

mockall_double

A double test adapter that works well with Mockall.

Build Status Crates.io Documentation

Overview

Mockall can easily create a mock version of a struct. But how does one convince the code under test to use the mock struct instead of the real one? In Rust, it's necessary to replace the real struct at compile time. That's very easy to do with a few #[cfg(test)] statements. But mockall_double makes it even easier.

Usage

Typically mockall is only used by unit tests, so it can be a dev-dependency. But mockall_double must be a full dependency. To use it this way, add this to your Cargo.toml:

[dependencies]
mockall_double = "0.1.0"

[dev-dependencies]
mockall = "0.8.0"

Then use it like this:

use mockall_double::double;

mod mockable {
    #[cfg(test)]
    use mockall::automock;

    pub struct Foo {}
    #[cfg_attr(test, automock)]
    impl Foo {
        pub fn foo(&self, x: u32) -> u32 {
            // ...
            0
        }
    }
}

#[double]
use mockable::Foo;

fn bar(f: Foo) -> u32 {
    f.foo(42)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bar_test() {
        let mut mock = Foo::new();
        mock.expect_foo()
            .returning(|x| x + 1);
        assert_eq!(43, bar(mock));
    }
}

See the API docs for more information.

Minimum Supported Rust Version (MSRV)

Mockall_double is tested with the same MSRV as Mockall itself. Currently, that's Rust 1.42.0. mockall_double's MSRV will not be changed in the future without bumping the major or minor version.

License

mockall_double is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE, and LICENSE-MIT for details

Acknowledgements

mockall_double is inspired by Jason Grlicky's double crate, but tweaked to work better with Mockall's naming conventions.

Dependencies

~335–760KB
~19K SLoC