agiledragon / gomonkey

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

ApplyMethod针对struct的私有方法无效,报错:retrieve method by name failed #63

Closed studyzy closed 1 year ago

studyzy commented 2 years ago

官方例子:TestApplyPrivate,运行结果,报错:

GOROOT=/usr/local/go #gosetup
GOPATH=/Users/devinzeng/go #gosetup
/usr/local/go/bin/go test -c -o /private/var/folders/h9/2yls4kmx2dj6v1fh2nlkfgww0000gn/T/GoLand/___TestApplyPrivate_in_github_com_agiledragon_gomonkey_v2_test.test github.com/agiledragon/gomonkey/v2/test #gosetup
/usr/local/go/bin/go tool test2json -t /private/var/folders/h9/2yls4kmx2dj6v1fh2nlkfgww0000gn/T/GoLand/___TestApplyPrivate_in_github_com_agiledragon_gomonkey_v2_test.test -test.v -test.paniconexit0 -test.run ^\QTestApplyPrivate\E$
=== RUN   TestApplyPrivate
E
Errors:

  * /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/test/apply_private_method_test.go 
  Line 25: - retrieve method by name failed 
  goroutine 18 [running]:
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:148 +0x5c
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:121 +0x70
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:235 +0x104
  panic({0x104eb5780, 0x104ee2ac0})
    /usr/local/go/src/runtime/panic.go:1038 +0x21c
  github.com/agiledragon/gomonkey/v2.(*Patches).ApplyMethod(0x14000125160, {0x104eeb398, 0x104ec0500}, {0x104e662c7, 0x2}, {0x104eb7c00, 0x104ee21b8})
    /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/patch.go:67 +0x1c0
  github.com/agiledragon/gomonkey/v2.ApplyMethod(...)
    /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/patch.go:27
  github.com/agiledragon/gomonkey/v2/test.TestApplyPrivate.func1.1()
    /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/test/apply_private_method_test.go:25 +0x160
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/discovery.go:80 +0x28
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:261 +0x144
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:163 +0x44
  github.com/jtolds/gls.(*ContextManager).SetValues.func1(0x0)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/context.go:97 +0x488
  github.com/jtolds/gls.EnsureGoroutineId(0x14000108690)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/gid.go:19 +0x110
  github.com/jtolds/gls.(*ContextManager).SetValues(0x14000104510, 0x14000108630, 0x140001280a8)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/context.go:63 +0x180
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:162 +0x364
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/doc.go:77 +0x94
  github.com/agiledragon/gomonkey/v2/test.TestApplyPrivate.func1()
    /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/test/apply_private_method_test.go:22 +0x6c
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/discovery.go:80 +0x28
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:261 +0x144
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:110 +0xe0
  github.com/jtolds/gls.(*ContextManager).SetValues.func1(0x0)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/context.go:97 +0x488
  github.com/jtolds/gls.EnsureGoroutineId.func1()
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/gid.go:24 +0x30
  github.com/jtolds/gls._m(0x0, 0x14000128090)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/stack_tags.go:108 +0x30
  github.com/jtolds/gls.github_com_jtolds_gls_markS(0x0, 0x14000128090)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/stack_tags.go:56 +0x30
  github.com/jtolds/gls.addStackTag(...)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/stack_tags.go:49
  github.com/jtolds/gls.EnsureGoroutineId(0x14000108540)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/gid.go:24 +0xdc
  github.com/jtolds/gls.(*ContextManager).SetValues(0x14000104510, 0x140001084e0, 0x1400014a080)
    /Users/devinzeng/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/context.go:63 +0x180
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:105 +0x288
    /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/doc.go:75 +0xb4
  github.com/agiledragon/gomonkey/v2/test.TestApplyPrivate(0x14000113380)
    /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/test/apply_private_method_test.go:21 +0x84
  testing.tRunner(0x14000113380, 0x104ee21d0)
    /usr/local/go/src/testing/testing.go:1259 +0x104
  created by testing.(*T).Run
    /usr/local/go/src/testing/testing.go:1306 +0x328

  goroutine 1 [chan receive]:
  testing.(*T).Run(0x140001131e0, {0x104e68b0a, 0x10}, 0x104ee21d0)
    /usr/local/go/src/testing/testing.go:1307 +0x344
  testing.runTests.func1(0x140001131e0)
    /usr/local/go/src/testing/testing.go:1598 +0x80
  testing.tRunner(0x140001131e0, 0x14000123d18)
    /usr/local/go/src/testing/testing.go:1259 +0x104
  testing.runTests(0x14000128048, {0x104fe6ba0, 0xa, 0xa}, {0x0, 0x0, 0x0})
    /usr/local/go/src/testing/testing.go:1596 +0x3ec
  testing.(*M).Run(0x1400014c000)
    /usr/local/go/src/testing/testing.go:1504 +0x4fc
  main.main()
    _testmain.go:61 +0x17c

1 total assertion

--- FAIL: TestApplyPrivate (0.00s)

FAIL

Process finished with the exit code 1

环境是Mac M1, go version go1.17 darwin/arm64

agiledragon commented 2 years ago

First of all, I recommend using the released version as much as possible. Secondly, we are implementing the private method in the main branch, and some of the problems we have encountered have not yet been resolved. Now that the test case has been skipped, let the test take effect after the feature is fully supported.

agiledragon commented 2 years ago

gomonkey has supported this feature in v2.3.1

studyzy commented 2 years ago

gomonkey has supported this feature in v2.3.1

newest master branch code run:

go test -gcflags=all=-l apply_private_method_test.go -v

not work, still show error:

=== RUN   TestApplyPrivateMethod

  TestApplyPrivateMethod 
    patch private pointer method in the different package 🔥
    patch private value method in the different package 🔥

Errors:

  * /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/test/apply_private_method_test.go 
  Line 17: - retrieve method by name failed 
  goroutine 4 [running]:
        /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:148 +0x5c
        /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:121 +0x70
        /Users/devinzeng/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:235 +0x104
  panic({0x1025fcb80, 0x102627dc0})
        /usr/local/go/src/runtime/panic.go:1038 +0x224
  github.com/agiledragon/gomonkey/v2.(*Patches).ApplyPrivateMethod(0x1400000c150, {0x10262da88, 0x10260c7a0}, {0x1025d3e07, 0x2}, {0x1025fefa0, 0x1026272d0})
        /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/patch.go:81 +0xa8
  github.com/agiledragon/gomonkey/v2.ApplyPrivateMethod({0x10262da88, 0x10260c7a0}, {0x1025d3e07, 0x2}, {0x1025fefa0, 0x1026272d0})
        /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/patch.go:32 +0x54
  command-line-arguments.TestApplyPrivateMethod.func1.1()
        /Users/devinzeng/go/src/github.com/agiledragon/gomonkey/test/apply_private_method_test.go:17 +0x7c
agiledragon commented 2 years ago

What is your go version?

studyzy commented 2 years ago

What is your go version?

go version go1.17 darwin/arm64

agiledragon commented 2 years ago

What is your go version?

go version go1.17 darwin/arm64

It turned out to be go1.17, which is exactly the same as I guessed. Please read the release notes carefully: https://github.com/agiledragon/gomonkey/releases At the same time, I also wrote an article before: https://www.jianshu.com/p/7546e788613b

go1.17 has not supported this feature temporarily, will be considered later.

Suvian-wy commented 2 years ago

Reference in

my go version is 1.16 linux/amd64,and i have installed the gomonkey 2.7.0. In the release notes, it say support the: go1.14 ok go1.15 ok go1.16 ok go1.17 ok go1.18 ok

but I also get the error panic: retrieve method by name failed [recovered]

different is: I use it for a public methods of private variables like this: patch := gomonkey.ApplyMethod(reflect.TypeOf(opt.Para.customer), "Query", func()....) .customer is the private variables

Suvian-wy commented 2 years ago

Reference in

my go version is 1.16 linux/amd64,and i have installed the gomonkey 2.7.0. In the release notes, it say support the: go1.14 ok go1.15 ok go1.16 ok go1.17 ok go1.18 ok

but I also get the error panic: retrieve method by name failed [recovered]

different is: I use it for a public methods of private variables like this: patch := gomonkey.ApplyMethod(reflect.TypeOf(opt.Para.customer), "Query", func()....) .customer is the private variables

sorry, I just use it in a wrong way. it's ok now