I have troubles figuring out unit tests for the methods of the target struct.
I have a method random_number
that returns a random value based on the att
Thanks to @Shepmaster I came to this workaround. I have added the actual Rng
to have more context.
use rand::{thread_rng, Rng}; // 0.6.5
struct RngTest(Vec);
impl RngTest {
fn random_number(&self) -> u64 {
let random_value = thread_rng().choose(&self.0);
*random_value.unwrap()
}
fn plus_one(&self) -> u64 {
self.random_number() + 1
}
}
#[test]
fn plus_one_works() {
let rng = RngTest(vec![1]);
assert_eq!(rng.plus_one(), 2);
}
I can set an appropriate value in the object and don't need to use traits. There is a downside though - this forces me to have a special instance of my type for this specific test which I would like to avoid because my actual type has a lot of fields and I wanted to define its creation once for all the tests using speculate
.