mockall_double

Crates.iomockall_double
lib.rsmockall_double
version0.3.1
sourcesrc
created_at2020-09-07 21:29:38.391369
updated_at2023-12-10 01:05:17.161448
descriptionTest double adapter for Mockall
homepage
repositoryhttps://github.com/asomers/mockall
max_upload_size
id285927
size26,641
Alan Somers (asomers)

documentation

https://docs.rs/mockall_double

README

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.

Commit count: 1040

cargo fmt