How to unit test expected failures? #254
Open
Comments
Would using fn main() {
match my_fn()
.expect_err("my_fn should return an error")
.downcast_ref()
.expect("the wrong type of error was returned")
{
MyError::BadThingHappened { badness_level: 9 } => (),
_ => panic!("the wrong error was returned"),
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I'm writing code, I often want to be able to test that it returns a the right error in a particular situation. With hand-implemented errors, I would use a
match
statement for this, since errors can't be expected to implementPartialEq
. Normally I would make this more informative and concise by using assert_matches from the matches crate. For example:How could I test this behavior with a
failure
error? For example:The text was updated successfully, but these errors were encountered: