agiledragon / gomonkey

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

Will panic if access interface method's parameters #115

Open grovecai opened 1 year ago

grovecai commented 1 year ago

Go Version: go version go1.18.2 darwin/amd64 MacOS version: macOS Monterey, version 12.3.1, Intel gomockey version: v2.9.0

Sample code

package go_play

import (
    "fmt"
    "github.com/agiledragon/gomonkey/v2"
    "testing"
)

type Dummy interface {
    Get(a string) string
}

type dummy struct {
}

func (t *dummy) Get(a string) string {
    return "prefix: " + a
}

func Test_go(t *testing.T) {
    patches := gomonkey.NewPatches()
    defer patches.Reset()
    patches.ApplyMethod(&dummy{}, "Get", func(_ Dummy, in string) string {
        fmt.Println("IN: " + in) //Panic
        return "dummy"
    })
    ret := (&dummy{}).Get("hello")
    fmt.Printf("Actual: " + ret)
}
grovecai commented 1 year ago

Panic with -gcflags=all=-l No Panic with -gcflags=-l, but fmt.Println("IN: " + in) not works as expected, only output IN: