agiledragon / gomonkey

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

Why ApplyFunc mocked func returns the same result in a for loop? #141

Closed hankeyyh closed 8 months ago

hankeyyh commented 8 months ago

I use gomonkey.ApplyFunc mock a func in for loop, then call it. Why it returns the same result every time.

func Test2(t *testing.T) {
    for i := 0; i < 3; i++ {
        gomonkey.ApplyFunc(env.Loc, func() int {
            tmp := i + 1
            return tmp
        })
        t.Logf("%p, %v", env.Loc, env.Loc())
    }
}

output:
0x8b85c0, 1
0x8b85c0, 1
0x8b85c0, 1
hankeyyh commented 8 months ago

solved, not using -gcflags=l