imocker is a tool designed to generate mock structs that implement an interface in order to effectively write unit tests using nothing but the standard library.
imocker generates actual Go code that can be used in unit tests without using strings to look up methods, without chaining function calls to compare expectations, and no ability to diverge from the interface implementation.
go install github.com/mvlipka/imocker@latest
imocker
imocker generate ./...
imocker generate ./testdata
A small example can be seen in the testdata
folder.
testdata/mock_thinger.go was generated by using imocker generate ./...
An interface such as
type Thinger interface {
MyFunc(param string) (string, error)
NamedReturn(multiple bool, types bool) (err error)
}
Will generate a mock such as:
type MockThinger struct {
TestMyFunc func(param string) (string, error)
TestNamedReturn func(multiple bool, types bool) (err error)
}
func (m *MockThinger) MyFunc(param string) (string, error) {
return m.TestMyFunc(param)
}
func (m *MockThinger) NamedReturn(multiple bool, types bool) (err error) {
return m.TestNamedReturn(multiple, types)
}
MyMethod(multiple, vars bool) (error)
- Methods with multiple parameters under a single type definition are unsupported
MyMethod(bool, bool) (error)
- Methods with no parameter names defined are unsupported