awterman / monkey

One line to mock functions/methods/variables in place without dependency injection or code generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Monkey - Gomonkey Generic

Go Reference Go Report Card Go Coverage Mentioned in Awesome Go

  • 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.

Usage:

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.

Notes

  • TL;DR: Be sure to add flag -gcflags=-l(below go1.10) or -gcflags=all=-l(go1.10 and above) in testing like go 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.

Improvements Compared to the Original

  • 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.

About

One line to mock functions/methods/variables in place without dependency injection or code generation

License:MIT License


Languages

Language:Go 55.7%Language:Shell 43.1%Language:Makefile 1.3%