Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApplyMethod failed to replace #170

Open
123jixinyu opened this issue Sep 4, 2024 · 3 comments
Open

ApplyMethod failed to replace #170

123jixinyu opened this issue Sep 4, 2024 · 3 comments

Comments

@123jixinyu
Copy link

123jixinyu commented Sep 4, 2024

package main

import (
	"fmt"
	"github.com/agiledragon/gomonkey/v2"
	. "github.com/smartystreets/goconvey/convey"
	"reflect"
	"testing"
)

type A struct {
	B *B
}

func (a *A) TestA() {
	fmt.Println(a.B.TestB())
	fmt.Println(2)
}

type B struct {
}

func (*B) TestB() int {
	return 1
}

func TestSpec(t *testing.T) {

	Convey("Test Spec", t, func() {

		b := &B{}
		patches := gomonkey.ApplyMethod(reflect.TypeOf(b), "TestB", func(_ *B) int {
			return 3
		})
		defer patches.Reset()

		a := A{B: b}
		a.TestA()
	})
}

output

1
2

but it should be 3 2 , why not works???

@LinxiItIs
Copy link

try '-gcflags=all=-N -l' to close inline

@123jixinyu
Copy link
Author

i tried it. it`s working now

@123jixinyu
Copy link
Author

try '-gcflags=all=-N -l' to close inline

extremely grateful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants