Idiomatic way of testing Result<T, Error>?


#1

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?


#2

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

}

http://is.gd/d8T8Jp


#3

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?


#4

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


#6

I like this.
Thanks!