gomonkey icon indicating copy to clipboard operation
gomonkey copied to clipboard

使用ApplyMethod替换gin.Context的方法未生效,为何?

Open listen-lavender opened this issue 4 years ago • 2 comments

import (
	"reflect"
	"testing"

	"github.com/agiledragon/gomonkey"
	"github.com/gin-gonic/gin"
	"github.com/golang/mock/gomock"
)

func TestSeq_GIN(t *testing.T) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	ginCtx := &gin.Context{
		Params: gin.Params{
			gin.Param{
				Key:   "ns",
				Value: "aaaa",
			},
		},
	}

	patches := gomonkey.ApplyMethod(reflect.TypeOf(ginCtx), "Param", func(_ *gin.Context, _ string) string {
		return "bbbb"
	})

	defer patches.Reset()

	println("===1", ginCtx.Param("ns"))
	t.Log("===2", ginCtx.Param("ns"))
}

go test -v 输出结果

image

listen-lavender avatar Jul 09 '20 09:07 listen-lavender

加上一个参数: go test -gcflags="all=-N -l"

https://github.com/agiledragon/gomonkey/issues/4#issuecomment-432503656

linqh1 avatar Jul 15 '20 09:07 linqh1

woca

wingyiu avatar Aug 31 '20 15:08 wingyiu