Automatically generates mock code from a Go interface.
This project is actively maintained. Commits are infrequent because it reached a certain degree of stability and no additional features are needed at this time.
$ go install github.com/vibridi/gomock/v3@latest
Go 1.22.0
Type gomock help for detailed usage tips. Mainly: gomock { help | [options] filename }
In short, it supports the following flags:
-f FILEallows to specify the input file where your interface is declared. If not provided, it's assumed the input file is the first argument after other options.-o FILEif set, tells the program to write the output toFILE. Otherwise it just prints to stdout. You can always capture the output with a pipe. E.g. if you are on MacOS, you could dogomock -f myfile.go | pbcopy-i IDENTIFIERif the input file contains more than one interface declaration, you can use the-iflag to tell the program which one to parse. If not set, the program defaults to the first encountered interface.-xif set, static functions are exported (usually those whose name begins withwithandnew)-uif set, allows to output default functions andWith*functions with unnamed arguments.-dif set, outputs top-levelwithFuncfunction identifiers with the name of the service. This is useful to avoid "function redeclared" errors when mocks of interfaces with identical method names exist in the same package. For example, if the service name isMyServicethenwithFuncDoSomethingbecomeswithFuncMyServiceDoSomething.--localif set, doesn't qualify output mock types with the package name. It qualifies them by default. The default behavior is to always output named arguments, as some IDEs reference them in code completion.--structif set, prints the output in struct style, instead of options style (see below for further details).--name NAMEallows to override the interface name used in output types withNAME.--utype MAPPING [ --utype MAPPING ]allows to manually specify the underlying type of a named type.--help, -hprints a help message.--version, -vprints the version number.
- The option
-qis removed. It's assumed that mocked types are always qualified with their package name. The option--localcan be used instead to opt out (i.e. to not qualify them). - The option
--struct-typehas been renamed to--struct. It has the same effect.
This tool is able to resolve composed interfaces, however all declarations must live
in the same directory or sub-directories relative to the main file. To see this in action, run make example-compose.
To try out the tool after cloning the repo:
$ make build
$ ./build/gomock -f _example/_example.go
It will print out the generated mock code, with the options pattern:
type mockTestInterface struct {
options mockTestInterfaceOptions
}
type mockTestInterfaceOptions struct {
funcGet func() string
funcSet func(v string)
}
var defaultMockTestInterfaceOptions = mockTestInterfaceOptions{
funcGet: func() string {
return ""
},
funcSet: func(string) {
return
},
}
type mockTestInterfaceOption func(*mockTestInterfaceOptions)
func withFuncGet(f func() string) mockTestInterfaceOption {
return func(o *mockTestInterfaceOptions) {
o.funcGet = f
}
}
func withFuncSet(f func(string) ) mockTestInterfaceOption {
return func(o *mockTestInterfaceOptions) {
o.funcSet = f
}
}
func (m *mockTestInterface) Get() string {
return m.options.funcGet()
}
func (m *mockTestInterface) Set(v string) {
return
}
func newMockTestInterface(opt ...mockTestInterfaceOption) TestInterface {
opts := defaultMockTestInterfaceOptions
for _, o := range opt {
o(&opts)
}
return &mockTestInterface{
options: opts,
}
}
Then you can use the generated code in your unit tests:
myMock := newMockTestInterface(
withFuncGet(f func() string {
return "test-value"
}),
)
myMock.Get() // "test-value"
objectThatUsesTestInterface := NewObject(myMock)
// ...
To print the mock code in struct style, run:
$ make build
$ ./build/gomock -f _example/_example.go --struct-style
And it will print:
type mockTestInterface struct {
GetFunc func() string
SetFunc func(v string)
}
func (m *mockTestInterface) Get() string {
if m.GetFunc != nil {
return m.GetFunc()
}
return ""
}
func (m *mockTestInterface) Set(v string) {
if m.SetFunc != nil {
m.SetFunc(v)
}
}
- Gabriele V. - Initial work and maintenance
Currently there are no other contributors
- Remove extra space between signature and
{when the function has no return types
This project is licensed under the MIT License - see the LICENSE.md file for details