gpt4 book ai didi

unit-testing - 消息带有撇号时,should_panic预期无法正常工作

转载 作者:行者123 更新时间:2023-12-03 11:46:29 26 4
gpt4 key购买 nike

我的代码失败,并显示以下消息"Didn't get a filename"但是我的should_panic没有检测到/匹配这个

#[test]
#[should_panic(expected="Didn't get a filename")]
产生结果
note: panic did not contain expected string
panic message: `"called `Result::unwrap()` on an `Err` value: \"Didn\\\'t get a filename\""`,
expected substring: `"Didn\'t get a filename"`
显然,解决方法是避免变音符号,但是我无法弄清楚如何转义匹配字符串。

最佳答案

Result::unwrap()要求错误类型实现Debug。因此,当创建紧急消息时,它将使用调试打印。对于字符串,该字符串将包含引号并转义某些字符:

fn main() {
let error = "Didn't get a filename";
println!("normal: {}", error);
println!("debug : {:?}", error);
}
normal: Didn't get a filename
debug : "Didn\'t get a filename"
似乎通常遵循 char::escape_default 的规则。
因此,在紧急消息中打印的实际字符串包括一个附加的 \:
#[test]
#[should_panic(expected = "Didn\\'t get a filename")]
fn panics() {
let result: Result<(), &str> = Err("Didn't get a filename");
result.unwrap();
}

关于unit-testing - 消息带有撇号时,should_panic预期无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66448642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com