Because you don't want to study the details of yet another framework, mokku
is a clipboard-based mocking framework for Go that gets out of your way.
This tool has been built with inspiration lovingly taken from Moq
, and fuelled by the frustration of using gomock
.
Key ideas:
- Invisible: No need to introduce dependencies or
//go:generate
directives in your codebase. - Integration friendly: Easy to integrate in your workflow, regardless of editor.
- File/package agnostic:
mokku
doesn't care where exactly your code lives on your system or even whether it compiles yet.
$ go get github.com/kinbiko/mokku/cmd/mokku
# Copy the interface
$ mokku
# Paste the mock
That's it. Below is an example of how to use mokku
within vim.
If you have been writing your own mocks or if you're familiar with
Moq
then the usage will look very familiar to you:
func TestRegisterUser(t *testing.T) {
var (
got = ""
exp = "kinbiko"
)
mock := &UserRepositoryMock{
CreateFunc: func(userName string) error {
got = userName
return nil
},
}
RegisterUser(exp, mock)
if got != exp {
t.Errorf("expected user name '%s' but got '%s'", exp, got)
}
}
func RegisterUser(userName string, repo UserRepository) {
// ... Code that includes a call to 'repo.Create(userName)'
}
You can also define your own custom template to use for your mocks, by defining MOKKU_TEMPLATE_PATH
that is the path to a file containing a Go text template, that uses the same variables found in the default template:
const defaultTemplate = `
type {{.TypeName}}Mock struct { {{ range .Methods }}
{{.Name}}Func func{{.Signature}}{{ end }}
}
{{if .Methods }}{{$typeName := .TypeName}}
{{range $val := .Methods}}func (m *{{$typeName}}Mock) {{$val.Name}}{{$val.Signature}} {
if m.{{$val.Name}}Func == nil {
panic("unexpected call to {{$val.Name}}")
}
{{if $val.HasReturn}}return {{ end }}m.{{$val.Name}}Func{{$val.OrderedParams}}
}
{{ end }}{{ end }}`
See the GoDocs for a more detailed explanation of the template variables.
Please raise an issue to discuss any changes you'd like to make to this project. If you wish to contribute, but need some ideas, please check out the GitHub project for this repository.
This project is has been made possible thanks to my employer who let me work on this project during Mercari Hack Week.