我遇到了如下问题:在编写单元测试时比较两个错误
package main
import (
"errors"
"fmt"
"reflect"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func main() {
er1 := errors.New("database name is not exists")
er2 := errors.New("database name is not exists")
result1 := reflect.DeepEqual(er1, er2)
fmt.Println("reflect: ", result1)
result2 := cmp.Equal(er1, er2, cmpopts.EquateErrors())
fmt.Println("compare: ", result2)
result3 := errors.Is(er1, er2)
fmt.Println("errorIs: ", result3)
}
上述代码的输出是:
reflect: true
compare: false
errorIs: false
我想比较两个错误,reflect.DeepEqual(er1, er2)
是我应用的第一个方法,这个方法产生了我想要的输出,但是这个方法有来自go lint
的警告
avoid using reflect.DeepEqual with errorsdeepequalerrors
在google搜索之后,有些人告诉我一些方法:
cmp.Equal(er1, er2, cmpopts.EquateErrors())
errors.Is(er1, er2)
但上述两种方法都不能产生与第一种方法相同的结果(使用reflect.DeepEqual)。如何在没有go lint
警告的情况下比较两个错误,并产生类似reflect.DeepEqual
Tks的结果
发布于 2021-04-28 10:18:05
根据您编写测试的方式,您可能依赖于reflect.DeepEqual()
而忽略linter警告;
缺点是:您根据返回的错误的内部结构而开始。
在我阅读的测试代码和我们编写的测试代码中,我们使用以下模式之一:
nil
;。
package pkg
var ErrUnboltedGizmo = errors.New("gizmo is unbolted")
// in test functions, depending on the case :
if err == pkg.ErrUnboltedGizmo { ...
// or :
if errors.Is(err, pkg.ErrUnboltedGizmo) { ...
当我们的生产代码要求发现一个特定的错误(一个常见的用例是)时,我们编写的代码忠实地包装了已知的错误,我们使用errors.Is()
(在生产代码和测试代码中都使用),在只需要测试的时候使用
Parse error
而不是File not found
),我们只需在错误消息中搜索字符串:< code >F 218
if err == nil || !strings.Contains(err.Error(), "database name is not exists") {
t.Errorf("unexpected error : %s", err)
}
发布于 2022-05-26 11:32:09
我发现有用的是使用cmp.Diff和cmpopts.IgnoreFields来忽略引起详细问题的特定错误字段,然后我就用string.Contains或任何我认为合适的方法对错误进行检查。
所以它是这样的:
if diff := cmp.Diff(expected, got, cmpopts.IgnoreFields(expected, "ErrorField")); diff != "" {
// found diff not including the error
}
现在只检查他们自己的错误,仅此而已。
是的,我知道你已经标记了一个解决方案,但也许它会对某人有所帮助:)
https://stackoverflow.com/questions/67297747
复制相似问题