紧急恐慌单元测试-匹配错误消息的详细信息

问题描述 投票:0回答:1

我正在寻找一种方法来断言一段代码恐慌,并且该恐慌消息包含特定的字符串。我想出了以下似乎可行的方法:

let actual = std::panic::catch_unwind(|| decode(notation.to_string()));
assert!(actual.is_err());
let err = *(actual.unwrap_err().downcast::<String>().unwrap());
assert!(err.contains("Invalid"));

[我知道我可以使用#[should_panic],它可以让我指定要检查的消息,但我只想部分匹配确切的错误消息。

testing rust panic
1个回答
2
投票

使用#[should_panic(expected = "substring of panic message")]

© www.soinside.com 2019 - 2024. All rights reserved.