agiledragon / gomonkey

gomonkey is a library to make monkey patching in unit tests easy
MIT License
1.93k stars 179 forks source link

ApplyMethod mock的目标是个interface,实际的函数调用是个struct,mock的时候报target type and double type are different #30

Open jingb opened 3 years ago

jingb commented 3 years ago

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 }

agiledragon commented 3 years ago

请参考apply_interface_reused_test.go中的示例代码,就知道如何对接口打桩了

yuqiandoudou commented 3 years ago

请问,如果 Mysql 首字符小写还能mock么? type mysql struct { }

func (this *mysql) Retrieve(url string) (string, error) { output := fmt.Sprintf("%s, %s!", "Hello", "Mysql") return output, nil }

agiledragon commented 3 years ago

首字母小写的类型能不能打桩,其实你试一下就知道了

yuqiandoudou commented 3 years ago

首字母小写的类型能不能打桩,其实你试一下就知道了

我试着不支持。这个后续会支持么?

agiledragon commented 3 years ago

视情况而定,关键是Go的反射机制是否支持这类标识符的查询。

yuqiandoudou commented 3 years ago

视情况而定,关键是Go的反射机制是否支持这类标识符的查询。

好的,多谢。

shimeng930 commented 2 years ago

从monkey过来 看了例子,需要指定具体实现结构体的一个实例; 这样不是可以直接用结构体的方法打桩了么? 我以为是可以直接对接口打桩

zhongyuanjia commented 1 year ago

已支持,见 https://github.com/agiledragon/gomonkey/blob/master/test/apply_interface_reused_test.go

0RAJA commented 1 year ago

接口打桩感觉官方的gomock就挺好用的https://github.com/golang/mock 对这种私有结构体的公开方法进行mock可以采用gomonkey.ApplyMethod