gomonkey
gomonkey copied to clipboard
ApplyMethod mock的目标是个interface,实际的函数调用是个struct,mock的时候报target type and double type are different
target type( func(*serverless.realServerlessControl, context.Context, string) error) and double type( func(*serverless.ServerlessControlInterface, context.Context, string) error) are different 其中 realServerlessControl实现了ServerlessControlInterface 这个interface,这种如何处理,望回复 感谢
type realServerlessControl struct { }
type ServerlessControlInterface interface { CreateFunction(ctx context.Context, functionId string) error }
请参考apply_interface_reused_test.go中的示例代码,就知道如何对接口打桩了
请问,如果 Mysql 首字符小写还能mock么? type mysql struct { }
func (this *mysql) Retrieve(url string) (string, error) { output := fmt.Sprintf("%s, %s!", "Hello", "Mysql") return output, nil }
首字母小写的类型能不能打桩,其实你试一下就知道了
首字母小写的类型能不能打桩,其实你试一下就知道了
我试着不支持。这个后续会支持么?
视情况而定,关键是Go的反射机制是否支持这类标识符的查询。
视情况而定,关键是Go的反射机制是否支持这类标识符的查询。
好的,多谢。
从monkey过来 看了例子,需要指定具体实现结构体的一个实例; 这样不是可以直接用结构体的方法打桩了么? 我以为是可以直接对接口打桩
已支持,见 https://github.com/agiledragon/gomonkey/blob/master/test/apply_interface_reused_test.go
接口打桩感觉官方的gomock就挺好用的https://github.com/golang/mock 对这种私有结构体的公开方法进行mock可以采用gomonkey.ApplyMethod