<Ct: rust> [ 2023-11-02 ] Rust Mocking Objects
Why
- 유닛 테스트 / 단위 테스트는 빠르게 끝나야 하고, 주변환경의 영향을 받지 않아야함.
So
mockall
라이브러리를 사용하여, 외부 결속성을 제거 하도록 변경
How
[dev-dependencies]
mockall = "*"
main.rs ``` rust
#[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)); } }
```