agiledragon / gomonkey

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

feature: replace the arguments of a function #111

Open eleztian opened 1 year ago

eleztian commented 1 year ago

Is there a more appropriate way to change the parameters of a function?

more complicated way....


var patchReadDir = gomonkey.NewPatches()
var readDir func(name string) ([]os.DirEntry, error)
readDir = func(name string) ([]os.DirEntry, error) {
        patchReadDir.Reset()
        defer patchReadDir.ApplyFunc(os.ReadDir, readDir)
        return os.ReadDir(filepath.Join(c.baseDir, name))
}
patchReadDir.ApplyFunc(os.ReadDir, readDir)
c.patches = append(c.patches, patchReadDir)
myzhan commented 1 year ago

I think it's related to https://github.com/agiledragon/gomonkey/issues/103