- Monkey is a generic implementation of the gomonkey.
- Monkey can be used in testing for stubbing.
- Very easy to use: only 3 function in all.
Patch a function:
p := Func(nil, fnFoo, func() string { return "bar" })
defer p.Reset()
if fnFoo() != "bar" {
t.Error("patch failed")
}
You can also patch global variables and methods with Var
and Method
. Check the tests for example.
- TL;DR: Be sure to add flag
-gcflags=-l
(below go1.10) or-gcflags=all=-l
(go1.10 and above) in testing likego test -gcflags=all=-l -v
- gomonkey fails to patch a function or a member method if inlining is enabled, please running your tests with inlining disabled by adding the command line argument that is -gcflags=-l(below go1.10) or -gcflags=all=-l(go1.10 and above).
- A panic may happen when a goroutine is patching a function or a member method that is visited by another goroutine at the same time. That is to say, gomonkey is not threadsafe. Do not call
t.Parallel
in your tests.
- Generic implementation instead of interface{} to provide type checks especially for functions/methods, which also benefits the code completion a lot.
- Only 3 functions to achieve all benefits from gomonkey.