登录
首页 >  Golang >  Go问答

编写单元测试以测试golang错误fs.PathError

来源:stackoverflow

时间:2024-02-15 19:09:24 312浏览 收藏

Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《编写单元测试以测试golang错误fs.PathError》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!


问题内容

我正在尝试捕获错误并对特定错误对象进行单元测试检查,这是我正在测试的 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.
...

到这里,我们也就讲完了《编写单元测试以测试golang错误fs.PathError》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>