bytedance / mockey

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

Origin(&ori) NOT works properly with struct methods #15

Closed Sychorius closed 1 year ago

Sychorius commented 1 year ago

Describe the bug

When using Mock(target).Origin(&ori), if target is a struct method, it will panic with type mismatch To Reproduce

type Foo struct{}

func (*Foo) Foo() {}

func TestXxx(t *testing.T) {
    mockey.PatchConvey("sample", t, func() {
        var origin func()
        mockey.Mock((*Foo).Foo).Origin(&origin).Build()
    })
}
// '*func()' and 'func(*tests.Foo)' mismatch

Expected behavior not panic, and origin should be the proper raw function of *Foo.Foo

Mockey version: v1.1.1

Environment: It can reporduce in any enviroment

Additional context When receiving this bug report, the reporter expects to mock a public method of a private struct in another package, as below:

// in 3rd package  
type foo struct{  private int /* private field */ }

func (f *foo) Foo() int {
   return f.private
}

func  NewFoo(i int) *foo { return &foo{ private: i } }
// in test package  
func TestXxx(t *testing.T) {
    mockey.PatchConvey("sample", t, func() {
        var origin func() int
        mockey.Mock(mockey.GetMethod(3rd.NewFoo(999), "Foo")).To(func() int { return origin() }).Origin(&origin).Build()
                3rd.NewFoo(123).Foo() // expected to return 123
    })
}

which means the first param of Foo (*foo) is created after mock.Build.