Idiomatic way of testing Result<T, Error>?


I am trying to write test cases for functions which return Result<T, Error>.
How can I write tests for checking T is returned or checking Error is returned?


Can’t you just try this?

fn check<T>(res: Result<T, Error>) {
    match res {
        Ok(val) => { println!("got the T") }
        Err(e) => { println!("got the Err") }



Yes, now I’m writing like the code below.

match fs::metadata(path) {
    Ok(_) => assert!(false, "db file should not exist"),
    Err(_) => assert!(true),

Is there a better way?


assert!(fs::metadata(path).is_err(), "db file should not exist")


I like this.


Found a better way which includes testing for specific errors