5 unstable releases

0.3.1 Dec 10, 2023
0.3.0 May 15, 2022
0.2.1 Dec 11, 2021
0.2.0 Dec 22, 2020
0.1.0 Sep 7, 2020

#256 in Testing

Download history 14444/week @ 2024-01-01 19548/week @ 2024-01-08 21893/week @ 2024-01-15 25691/week @ 2024-01-22 21856/week @ 2024-01-29 23999/week @ 2024-02-05 27439/week @ 2024-02-12 20799/week @ 2024-02-19 30314/week @ 2024-02-26 27758/week @ 2024-03-04 32632/week @ 2024-03-11 28718/week @ 2024-03-18 33292/week @ 2024-03-25 36595/week @ 2024-04-01 32401/week @ 2024-04-08 35168/week @ 2024-04-15

138,417 downloads per month
Used in 36 crates (30 directly)

MIT/Apache

15KB
217 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.3.1"

[dev-dependencies]
mockall = "0.12.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

~290–740KB
~18K SLoC