小编rus*_*ron的帖子

我怎样才能测试stdin和stdout?

我想编写一个提示函数,将传入的字符串发送到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)

testing rust

7
推荐指数
1
解决办法
1786
查看次数

标签 统计

rust ×1

testing ×1