-
Notifications
You must be signed in to change notification settings - Fork 8
Description
First, thanks for your work on this package that is restricting the API to exactly what most users are in need for.
be.Err can currently accept:
If want is nil, checks if got is nil.
If want is a string, checks if got.Error() contains want.
If want is an error, checks if its value is found in the got's error tree using [errors.Is](https://pkg.go.dev/errors#Is).
If want is a [reflect.Type](https://pkg.go.dev/reflect#Type), checks if its type is found in the got's error tree using [errors.As](https://pkg.go.dev/errors#As).
Otherwise fails the check.
However, in parametric (table) testing, you often want to assert that there is an error, but you don’t mind which it is. You can do this with current API with an empty string, which I find confusing personnally (empty string should probably no error). Also, this will force an
if tc.wantErr {
// do the assertions
} else {
be.Err(t, err, nil)
}
I would like to suggest an extension of be.Err to optionally take an boolean too, that way, we could replace the above scenario in which we don’t care of the type of error we got with a one liner:
be.Err(t, err, tc.wantErr)
In case you mind about the error, but still only want one liner assertion, if we replace the empty string to be "I want an no error", then we can also do it with:
be.Err(t, err, tc.wantSpecificErr)
where wantSpecificErr can be empty in the struct of the table test to assert against no error.
This accepts both "we want an error, whatever it is" in that scenario but also "we want to ensure we don't have an error before doing the next assertions" (thanks for making be.Err() failing the test right away btw!)
If this is acceptable in your design, I am happy to work on it and propose a PR.