goo*_*gic 1 testing panic rust
我正在寻找一种方法来断言一段代码恐慌,并且恐慌消息包含一个特定的字符串。我想出了以下似乎有效的方法:
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"));
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用#[should_panic]
并且它让我指定要检查的消息,但我只想部分匹配确切的错误消息。
用 #[should_panic(expected = "substring of panic message")]
例子
fn some_panic_function() {
panic!("abc substring of panic message abc");
}
#[test]
#[should_panic(expected = "substring of panic message")]
fn test_some_panic_function() {
some_panic_function();
}
Run Code Online (Sandbox Code Playgroud)
从这个https://doc.rust-lang.org/book/ch11-01-writing-tests.html
归档时间: |
|
查看次数: |
595 次 |
最近记录: |