| 1 | # mockall_double |
| 2 | |
| 3 | A double test adapter that works well with Mockall. |
| 4 | |
| 5 | [](https://cirrus-ci.com/github/asomers/mockall) |
| 6 | [](https://crates.io/crates/mockall_double) |
| 7 | [](https://docs.rs/mockall_double) |
| 8 | |
| 9 | ## Overview |
| 10 | |
| 11 | Mockall can easily create a mock version of a struct. But how does one |
| 12 | convince the code under test to use the mock struct instead of the real one? |
| 13 | In Rust, it's necessary to replace the real struct at compile time. That's |
| 14 | very easy to do with a few `#[cfg(test)]` statements. But mockall_double makes |
| 15 | it even easier. |
| 16 | |
| 17 | ## Usage |
| 18 | |
| 19 | Typically `mockall` is only used by unit tests, so it can be a dev-dependency. |
| 20 | But `mockall_double` must be a full dependency. To use it this way, add this to |
| 21 | your `Cargo.toml`: |
| 22 | |
| 23 | ```toml |
| 24 | [dependencies] |
| 25 | mockall_double = "0.3.1" |
| 26 | |
| 27 | [dev-dependencies] |
| 28 | mockall = "0.12.0" |
| 29 | ``` |
| 30 | |
| 31 | Then use it like this: |
| 32 | |
| 33 | ```rust |
| 34 | use mockall_double::double; |
| 35 | |
| 36 | mod mockable { |
| 37 | #[cfg(test)] |
| 38 | use mockall::automock; |
| 39 | |
| 40 | pub struct Foo {} |
| 41 | #[cfg_attr(test, automock)] |
| 42 | impl Foo { |
| 43 | pub fn foo(&self, x: u32) -> u32 { |
| 44 | // ... |
| 45 | 0 |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #[double] |
| 51 | use mockable::Foo; |
| 52 | |
| 53 | fn bar(f: Foo) -> u32 { |
| 54 | f.foo(42) |
| 55 | } |
| 56 | |
| 57 | #[cfg(test)] |
| 58 | mod tests { |
| 59 | use super::*; |
| 60 | |
| 61 | #[test] |
| 62 | fn bar_test() { |
| 63 | let mut mock = Foo::new(); |
| 64 | mock.expect_foo() |
| 65 | .returning(|x| x + 1); |
| 66 | assert_eq!(43, bar(mock)); |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | See the [API docs](https://docs.rs/mockall_double) for more information. |
| 72 | |
| 73 | # Minimum Supported Rust Version (MSRV) |
| 74 | |
| 75 | `mockall_double` is tested with the same MSRV as Mockall itself. Currently, |
| 76 | that's Rust 1.42.0. `mockall_double`'s MSRV will not be changed in the future |
| 77 | without bumping the major or minor version. |
| 78 | |
| 79 | # License |
| 80 | |
| 81 | `mockall_double` is primarily distributed under the terms of both the MIT |
| 82 | license and the Apache License (Version 2.0). |
| 83 | |
| 84 | See LICENSE-APACHE, and LICENSE-MIT for details |
| 85 | |
| 86 | # Acknowledgements |
| 87 | |
| 88 | `mockall_double` is inspired by Jason Grlicky's |
| 89 | [double](https://crates.io/crates/double) crate, but tweaked to work better |
| 90 | with Mockall's naming conventions. |