Why
- Unit tests have to finish fast and must not be affected by their surroundings.
So
- Use the
mockallcrate to strip out external coupling.
How
[dev-dependencies]
mockall = "*"
main.rs
#[cfg(test)]
use mockall::{automock, mock, predicate::*};
#[cfg_attr(test, automock)]
trait MyTrait {
fn foo(&self, x: u32) -> u32;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mytest() {
let mut mock = MockMyTrait::new();
mock.expect_foo()
.with(eq(4))
.times(1)
.returning(|x| x + 1);
assert_eq1!(5, mock.foo(4));
}
}