agiledragon / gomonkey

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

ApplyPrivateMethod return interface is invalid in 1.16 #118

Open maodou1990 opened 1 year ago

maodou1990 commented 1 year ago

PrivateMethodStruct add two private methods:

func (this *PrivateMethodStruct) lastError() error{
    return nil
}

func (this *PrivateMethodStruct) LastError() error{
    return this.lastError()
}

TestApplyPrivateMethod add test below:

Convey("patch private method return interface", func() {
    f := new(fake.PrivateMethodStruct)
    var s *fake.PrivateMethodStruct
    pErr := errors.New("error")
    patches := ApplyPrivateMethod(s, "lastError", func() error {
        return pErr
    })
    defer patches.Reset()
    result := f.LastError()
    So(result, ShouldEqual, pErr)
})

we got a panic: invalid memory address or nil pointer dereference