agiledragon / gomonkey

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

ApplyMethod Invalid with -gcflags=all=-l in beego framework controller.ServeJSON() #44

Open kirinshow opened 3 years ago

kirinshow commented 3 years ago

my environment:

As my title description, it work well until i encountered the method :

func (c *Controller) ServeJSON(encoding ...bool) {
    var (
        hasIndent   = BConfig.RunMode != PROD
        hasEncoding = len(encoding) > 0 && encoding[0]
    )

    c.Ctx.Output.JSON(c.Data["json"], hasIndent, hasEncoding)
}

even though i use the -gcflags=all=-l , it's doesn't work as well. There is my test code:

var c *myController
patches := ApplyMethod(reflect.TypeOf(c), "ServeJSON",func(*myController, ...bool) {
    //do nothing
})
defer patches.Reset()

see the result as below :

panic(0x494ab60, 0x53e96d0)
    /usr/local/go/src/runtime/panic.go:969 +0x191
  github.com/astaxie/beego/context.(*BeegoOutput).Header(0xc00031e6a0, 0x4a7cca5, 0xc, 0x4a93a96, 0x1f)
    /Users/xxx/go/src/github.com/astaxie/beego/context/output.go:58 +0x25
  github.com/astaxie/beego/context.(*BeegoOutput).JSON(0xc00031e6a0, 0x49c2ba0, 0xc00037ca20, 0xc000380000, 0xc00038a128, 0xc00038a120)
    /Users/xxx/go/src/github.com/astaxie/beego/context/output.go:189 +0x6a
  github.com/astaxie/beego.(*Controller).ServeJSON(0xc0001c42a0, 0x0, 0x0, 0x0)
    /Users/xxx/go/src/github.com/astaxie/beego/controller.go:357 +0xdc

How to fix this problem or recommand me a HTTP unit test framework ? Thanks!!!!

agiledragon commented 3 years ago

What is the relationship between "Controller" and "myController"?

kirinshow commented 3 years ago

What is the relationship between "Controller" and "myController"

type MyController struct {
    beego.Controller
}

based on the beego framework,MyController inherit from beego.Controller which is the Controller