我想编写一个提示函数,将传入的字符串发送到stdout,然后返回它从stdin读取的字符串.我怎么测试呢?
以下是该功能的示例:
fn prompt(question: String) -> String {
let mut stdin = BufferedReader::new(stdin());
print!("{}", question);
match stdin.read_line() {
Ok(line) => line,
Err(e) => panic!(e),
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试尝试
#[test]
fn try_to_test_stdout() {
let writer: Vec<u8> = vec![];
set_stdout(Box::new(writer));
print!("testing");
// `writer` is now gone, can't check to see if "testing" was sent
}
Run Code Online (Sandbox Code Playgroud)