需要单元测试 golang 错误 fs.PathError

Need to unittest golang error fs.PathError

我正在尝试捕获错误并对特定错误对象进行单元测试检查,这里是 prod 函数我正在测试:

const command = "/tmp/cmd"

type RedisAdapter struct {
    modeStatus bool
}

func (r RedisAdapter) Enable(client domain.client) error {
    cmdArgs := []string{
        "redis-job-shard-pause",
        strconv.Itoa(client.Shard()),
        strconv.Itoa(client.Duration()),
    }
    cmd := fmt.Sprintf("%v", command)
    out, err := exec.Command(cmd, cmdArgs...).Output()
    fmt.Printf("%v", string(out))
    return err
}

单元测试代码,运行 测试 error=nil 和 error=fs.PathError:

func TestEnableService_SetEnable(t *testing.T) {
    type fields struct {
        LoadEnablePort out.EnablePort
    }
    type args struct {
        client domain.Client
    }
    fileNotFound := fs.PathError{
        Op:   "fork/exec",
        Path: "/tmp/cmd",
        Err:  syscall.ENOENT,
    }
    tests := []struct {
        name   string
        fields fields
        args   args
        want   error
    }{
        {
            "Enable Redis",
            fields{
                LoadEnablePort: redis.RedisAdapter{},
            },
            args{
                client: domain.Client{},
            },
            nil,
        },
        {
            "enable_redis_cmd_not_found",
            fields{
                LoadEnablePort: redis.RedisAdapter{},
            },
            args{
                client: domain.Client{},
            },
            &fileNotFound,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            s := EnableService{
                LoadEnablePort: tt.fields.LoadEnablePort,
            }
            if got := s.LoadEnablePort.Enable(tt.args.client); got != tt.want {
                t.Errorf("LoadEnablePort.RedisAdapter.Enable = %v, want %v", got, tt.want)
            }
        })
    }
}

nil 测试工作正常,但是当我进行第二次测试时,即使对象在调试器中显示完全相同也无法说明测试失败的原因:

https://i.stack.imgur.com/CTtfk.png

注意“got”变量如何与“tt.want”变量对象完全相同,最后我有以下失败的测试:

=== RUN   TestEnableService_SetEnable/enable_redis_cmd_not_found
    setModeService_test.go:116: LoadEnablePort.RedisAdapter.Enable = fork/exec /tmp/cmd: no such file or directory, want fork/exec /tmp/cmd: no such file or directory
    --- FAIL: TestEnableService_SetEnable/enable_redis_cmd_not_found (549.32s)

非常感谢您的帮助。

两个错误值是可比较的,它们包含指向 fs.PathError 结构的指针。

他们的地址不同,所以不一样。

如果您取消引用它们,则它们相等。

https://play.golang.org/p/ZMoEahbyIX_E

您应该使用 errors.Iserrors.As

此处指定相等运算符https://golang.org/ref/spec#Comparison_operators

它说

...
Interface values are comparable. 
Two interface values are equal if they have 
identical dynamic types and equal dynamic values 
or if both have value nil.
...