I have a trait like such:
#[cfg_attr(test, automock)]
trait Foo {
#[cfg_attr(test, concretize)]
fn for_each<F>(&self, processor: F)
where
Self: Sized,
F: FnMut(&u32);
}
#[derive(Default)]
struct UnderTest;
impl UnderTest {
fn do_something(&self, foo: &Foo) {
foo.for_each(|x| {
eprintln!("Invoked with {x}");
})
}
}
In my test I want to do something like:
#[cfg(test)]
fn do_something() {
let mut foo = MockFoo::new();
foo.expect_for_each().times(1).returning(|cb| {
cb(&0);
cb(&1);
});
let t = UnderTest::default();
t.do_something(&foo);
}
The problem is that when I compile this, there’s an error because the returning
closure receives a &FnMut
instead of a &mut FnMut
so there’s no way to invoke it. Am I missing something obvious or is it impossible to actually mock this situation?