【问题标题】:Golang mock for all arguments exceptGolang 模拟所有参数,除了
【发布时间】:2020-01-21 20:21:28
【问题描述】:
mock.on("FunctionName", "someStringArgument").Return(...)

假设如果someStringArgument 是“hello”,那么我想返回“1”。但是如果someStringArgument 是任何其他字符串,我想返回“2”。

GoMock 是如何实现的?

【问题讨论】:

标签: go


【解决方案1】:

您要做的是编写一个自定义函数,该函数将返回您想要的输出。

这是我所做的一个简单示例。

定义自定义响应函数

func FunctionNameResponse(arg String) string{
    if arg == "hellp" {
        // I used quotes because you mentioned "1" and not 1
        return "1"
    }
    return "2"
}

在任何需要的地方调用自定义函数。

mock.on("FunctionName", mock.Anything).Return(FunctionNameResponse("someStringArgument"))

【讨论】:

    猜你喜欢
    • 2015-01-14
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2020-07-16
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多