I will simplify my use case and implementation to make my question brief.
There are 3 functions in my lib.rs
- fetch_words_from_database() -> Vec<&'a str>
- fetch_words_from_api() -> Vec<&'a str>
- find_intersection() -> Vec<&'a str>
I want to unit test find_intersection function so I have to mock functions 1 and 2. How to achieve that?
fn find_intersection() -> Vec<&'a str> {
let words_database = fetch_words_from_database();
let words_api = fetch_words_from_api();
let words_api: HashSet<_> = words_api.into_iter().collect();
let mut intersection: Vec<&'a str> = Vec::new();
for word in words_database {
if words_api.contains(word) {
intersection.push(word)
}
}
intersection
}
I am looking for something like jest.spyOn.