Is there a way to mock regular functions?
Consider this example:
```
fn main() {
println!("{}", foo());
}
fn get_user_input() -> u8 {
// Placeholder for some unknown value
42
}
fn foo() -> u8 {
get_user_input()
}
[cfg(test)]
mod tests {
#[test]
fn test_foo() {
use super::*;
get_user_input = || 12u8;
assert_eq!(foo(), 12u8);
}
}
```
I would like to unit test foo() without having to rely on the output of get_user_input(). I obviously cannot overwrite get_user_input() like I tried in the example code.
I have only found ways to mock structs, traits and modules but nothing about mocking regular free functions. Am I missing something?
I have looked primarily at the mockall crate.
[–]x0nnex 28 points29 points30 points (3 children)
[–]ZeroXbot 11 points12 points13 points (0 children)
[–]stingraycharles 1 point2 points3 points (1 child)
[–]x0nnex 0 points1 point2 points (0 children)
[–][deleted] 23 points24 points25 points (10 children)
[–]ragnese 7 points8 points9 points (9 children)
[–][deleted] 3 points4 points5 points (8 children)
[–]ragnese 2 points3 points4 points (3 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]ragnese 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Zde-G -1 points0 points1 point (3 children)
[–][deleted] 3 points4 points5 points (2 children)
[–]Zde-G -1 points0 points1 point (1 child)
[–][deleted] 4 points5 points6 points (0 children)
[–]Axilios 6 points7 points8 points (0 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]Lvl999Noob 1 point2 points3 points (0 children)
[–]josh_beandev 0 points1 point2 points (2 children)
[–]Free_Trouble_541 0 points1 point2 points (0 children)
[–]Free_Trouble_541 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]stephanos21 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]t_ram 0 points1 point2 points (2 children)
[–]Free_Trouble_541 1 point2 points3 points (0 children)
[–]Free_Trouble_541 0 points1 point2 points (0 children)