Skip to content

Instantly share code, notes, and snippets.

Last active Jan 26, 2021
What would you like to do?
Monkey Patch Mock in Golang
package service
import (
pwi "pkg-without-interface"
type service struct {}
func (s *service) ListPosts() []Post {
posts := pwi.ListPosts() // ListPosts is a pkg function.
// ...
return posts
type Post struct {}
package service
import (
func TestListPosts(t *testing.T) {
guard := monkey.Patch(pwi.ListPosts, func() []Post {
fmt.Println("what the hell?") // what the *bleep*?
// ...
return []Post{}
defer guard.Unpatch()
serivce := &service{}
assert.Equal(t, []Post{}, service.ListPosts())
Copy link

lazywhite commented Jan 26, 2021

[root@control-plane monkey]# go run -N -l main.go
flag provided but not defined: -N
usage: go run [build flags] [-exec xprog] package [arguments...]
Run 'go help run' for details.

-N -1 not working, golang-1.14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment