dimanche 20 octobre 2019

Rust panic unit test compare string message

I'm looking for a way to assert that a piece of code panics, and that the panic message contains a particular string. I came up with the following which appears to work:

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"));

But surely there must be a better way (less verbose? more idiomatic?)

Aucun commentaire:

Enregistrer un commentaire