Use the mockall crate to generate mock implementations of traits or structs for unit testing. Add mockall to your Cargo.toml under [dev-dependencies], then use the mock! macro to define your mock struct and configure expectations in your tests.
[dev-dependencies]
mockall = "0.13.0"
use mockall::mock;
mock! {
pub File {
pub fn inner_read(&self, dst: &mut [u8]) -> std::io::Result<usize>;
}
}
#[test]
fn test_read() {
let mut mock = MockFile::default();
mock.expect_inner_read().returning(|_| Ok(5));
let result = mock.inner_read(&mut [0; 10]);
assert_eq!(result.unwrap(), 5);
}