agiledragon / gomonkey

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

gomonky 为泛型结构体的方法打桩不生效 #161

Closed zzBeeCoder closed 3 months ago

zzBeeCoder commented 3 months ago

go 1.20 gomonkey v2.1.0

type AbstractCall[T any] struct {
}

func (m *AbstractCall[T]) Call(a T) string {
    return "abstract call"
}
func main() {
    defer gomonkey.ApplyMethod(reflect.TypeOf(&AbstractCall[string]{}), "Call",
        func(_ *AbstractCall[string], a string) string {
            return "gomonkey call:" + a
        }).Reset()
    m := &AbstractCall[string]{}
    fmt.Println("call:" + m.Call("aa"))
}

image

xhd2015 commented 3 months ago

范型的支持可以参考xgo实现:https://github.com/xhd2015/xgo

zzBeeCoder commented 3 months ago

参考作者文章:https://www.jianshu.com/p/8a52eae7f786