agiledragon / gomonkey

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

方法体内的执行代码太少的时后不会生效 #5

Closed moshuipan closed 5 years ago

moshuipan commented 5 years ago
package main

import (
    "fmt"
    "reflect"

    "github.com/agiledragon/gomonkey"
)
type B struct {
    a string
}
func (b *B) Get(s string) string {
    return ""
}
func (b *B) Put(s string) string {
    var i int
    for ; i < 100; i++ {
        b.a = "iiiii"
    }
    return ""
}
func main(){
    var b *B
    gomonkey.ApplyMethod(reflect.TypeOf(b), "Get",
        func(_ *B, subPath string) string {
            return subPath
        })
    fmt.Println("get:", ws.Get("get")) // want "get"

    gomonkey.ApplyMethod(reflect.TypeOf(b), "Put",
        func(_ *B, subPath string) string {
            return subPath
        })
    fmt.Println("put:", ws.Put("put")) 
}

b.Get没被替换

agiledragon commented 5 years ago

请看README中Notes的第一点

moshuipan commented 5 years ago

ok,thanks:smile: