kmdreko's hint seems to work. Oracle now owns RC<RefCell>
pub struct Oracle {
source: Rc<RefCell<dyn WisdomSource>>,
}
impl Oracle {
pub fn new() -> Oracle {
Oracle {
source: Rc::new(RefCell::new(Magic8Ball))
}
}
pub fn ask(&self, question: &String) -> String {
self.source.borrow_mut().ask(question)
}
}
Then at unit test, I will holdon to RC of RefCell of the mock and inject clone of that RC downcasted to Oracle
#[test]
fn test_sage() {
let mockup_rc = Rc::new(RefCell::new(MockWisdomSource::new()));
mockup_rc.borrow_mut().expect_ask().times(1).return_const("42".to_string());
let source = Rc::clone(&mockup_rc) as Rc<RefCell<dyn WisdomSource>>;
let sage = Oracle{source};
let answer = sage.ask(&"What is the meaning of life?".to_string());
assert_eq!(answer, "42");
// Check that the mockup was called
mockup_rc.borrow_mut().checkpoint();
}