bytedance / mockey

a simple and easy-to-use golang mock library
Apache License 2.0
556 stars 22 forks source link

panic when GetMethod on interface{} #19

Closed Sychorius closed 1 year ago

Sychorius commented 1 year ago

Describe the bug

panic when GetMethod on interface{} with message: reflect: call of reflect.Value.Interface on zero Value

To Reproduce

run this test


type A interface {
    Foo()
}

type a struct {
    A
}

type b struct {
    i int
}

func (*b) Foo() {
    fmt.Println("abc")
}

func NewA() A {
    return &a{A: new(b)}
}

func TestXxx(t *testing.T) {
    mockey.PatchConvey("sample", t, func() {
        obj := NewA()
        mockey.Mock(mockey.GetMethod(obj, "Foo")).Build()
    })
}

Expected behavior

Not panic, mock successfully

Mockey version:

v1.2.0

Sychorius commented 1 year ago

This is because getNestedMethod use reflect.Type not reflect.Value, which loss the interface{} field context. We will fix it soon