gomonkey icon indicating copy to clipboard operation
gomonkey copied to clipboard

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

Open jingb opened this issue 4 years ago • 9 comments

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 }

jingb avatar Sep 27 '20 12:09 jingb

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

agiledragon avatar Oct 01 '20 11:10 agiledragon

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

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

yuqiandoudou avatar Jul 02 '21 12:07 yuqiandoudou

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

agiledragon avatar Jul 04 '21 14:07 agiledragon

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

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

yuqiandoudou avatar Jul 06 '21 10:07 yuqiandoudou

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

agiledragon avatar Jul 06 '21 14:07 agiledragon

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

好的,多谢。

yuqiandoudou avatar Jul 13 '21 03:07 yuqiandoudou

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

shimeng930 avatar Jul 03 '22 09:07 shimeng930

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

zhongyuanjia avatar Jul 25 '22 16:07 zhongyuanjia

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

0RAJA avatar Apr 07 '23 02:04 0RAJA