给定下面使用了mockall库的测试上下文,我怎样才能满意地返回一个对cmd
的引用,使我能够对随后的链接方法调用做出断言呢?
mock! {
Cmd {}
impl CustomCommand for Cmd {
fn args(&mut self, args: &[String]) -> &mut Self;
fn output(&mut self) -> std::io::Result<Output>;
fn spawn(&mut self) -> std::io::Result<std::process::Child>;
fn status(&mut self) -> std::io::Result<ExitStatus>;
}
}
#[test]
fn test_x() {
let mut cmd = MockCmd::new();
cmd.expect_args().times(1).returning(|_| &mut cmd);
// results in:
// expected `MockCmd`, found `&mut MockCmd`
cmd.expect_args().times(1).returning(|_| cmd);
// results in:
// move occurs because `cmd` has type `tests::MockCmd`, which does not implement the `Copy` trait
// NOTE: I don't think I _can_ implement Copy for my struct because it's a newtype on top of Command
cmd.expect_output()
.times(1)
.returning(|| Ok(create_output(0, vec![], vec![])));
// Call the methods in the desired order
let args = vec![String::from(":P")];
let _x = cmd.args(&args).output();
}