【发布时间】:2021-10-07 11:48:33
【问题描述】:
我正在尝试使用 GoMonkey 模拟一个函数,但我发现它失败了,因为编译器会在程序编译时内联这个函数,所以 GoMonkey 无法模拟它。
那么我该如何模拟这个函数或如何模拟其他成员的私有函数?
测试代码如下:
patches := gomonkey.ApplyFunc(time.ParseInLocation, func(format string, timeStr string, cstSh *time.Location) (time.Time,error) {
return time.Time{},errors.New("error")
})
defer patches.Reset()
resp:= Example("")
So(resp, ....)
模拟函数
...
t, err := time.ParseInLocation(format, timeStr, cstSh)
...
【问题讨论】:
-
停止这种嘲弄的废话。 Mocking 是一种来自 Java/C#/PHP 的技术,它不能与 Go 很好地配合。
-
1.不要使用模拟。 2. 即使你确实使用了模拟,也要使用适当的模拟库,而不是 GoMonkey。
标签: go go-testing