Crates.io | mockall_double |
lib.rs | mockall_double |
version | 0.3.1 |
source | src |
created_at | 2020-09-07 21:29:38.391369 |
updated_at | 2023-12-10 01:05:17.161448 |
description | Test double adapter for Mockall |
homepage | |
repository | https://github.com/asomers/mockall |
max_upload_size | |
id | 285927 |
size | 26,641 |
A double test adapter that works well with Mockall.
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.
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.
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.
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
mockall_double
is inspired by Jason Grlicky's
double crate, but tweaked to work better
with Mockall's naming conventions.