go - 如何处理随机输入输出值

标签 go gomock

我有一个定义 Downloadfile 函数的包装器接口(interface)。问题是其中一个参数有一个随机的临时目录名,而输出是一个随机的临时文件名。我如何使用 https://github.com/golang/mock 模拟它这样我就可以测试 addFile 函数了

var wrapper
func Test(t *testing.T) {
    ctrl, _ := gomock.WithContext(context.Background(), t)
    defer ctrl.Finish()
    m := mock.NewMockWrapperInterface(ctrl)
    m.EXPECT().DownloadFile("myfile", "/var/folders/ls/???").Return("/var/folders/ls/???", nil)
    wrapper = m
    path, err := addFile(myfile, key)
    ...
}

func addFile(myfile, key) (string, error) {
    tmpDirectory := helper.CreateTemporaryDirectory(objectKey)
    defer helper.CleanUpDirectory(tmpDirectory)
    return wrapper.DownloadFile(myfile, tmpDirectory)
}

最佳答案

了解匹配器的工作原理。这是我需要做的

m.EXPECT().DownloadFile("myfile", gomock.Any()).Return("/var/folders/ls/ok", nil)

关于go - 如何处理随机输入输出值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51920705/

相关文章:

mongodb - 从Golang中的bson.M中过滤出值

unit-testing - 使用 Gomock 测试返回错误 : Expected call has already been called the max number of times

unit-testing - 测试和模拟一个不返回任何值的函数

使用 for 循环和匿名函数进行并发时出现意外行为

go - Go 中如何将字节数组转换为字符串数组?

json - 数组的元素之一作为对象

unit-testing - 如何确保所有异步函数在执行 defer 之前运行

unit-testing - 如何使用 gomock (或类似的)来模拟/验证对数据库的调用?

json - 如何从 golang-map 中检索数据