agiledragon / agiledragon/gomonkey

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

Đang mở
#22 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Go
Star
2.3k
Fork
192
Merge trung bình
1 ngày 6 giờ
Pull request đã merge (30 ngày)
1

Mô tả

```go
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](https://user-images.githubusercontent.com/4168558/87021135-65ff2100-c207-11ea-836c-bb9bc257ab8c.png)

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.