You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
mock is a code generation tool for creating simple mock implementations ofinterfaces for use in testing.
Mocks are thread-safe.
Installation
You can installmock locally using the following command:
go install github.com/nicheinc/mock
However, if your Go module depends onmock, you may wish to add it to yourgo.mod as a tool dependency:
go get -tool github.com/nicheinc/mock
If you also vendor your dependencies, this ensures that contributors to yourmodule can create or update its mocks reproducibly usinggo tool mock,regardless of which version ofmock they may or may not have installedlocally.
Usage
Usage: mock [options] [interface]When the positional interface argument is omitted, all interfaces in the searchdirectory annotated with a "go:mock [output file]" directive will be mocked andoutput to stdout or, with the -w option, written to files. If a go:mockdirective in a file called example.go doesn't specify an output file, thedefault output file will be the -o flag (if provided) or else example_mock.go.When an interface name is provided as a positional argument after all otherflags, only that interface will be mocked. The -w option is incompatible with aninterface argument.Options: -d string Directory to search for interfaces in (default ".") -o string Output file (default stdout) -w Write mocks to files rather than stdout
Example
Given this interface (note the specialgo:mock directive) in a file calledgetter.go:
mock will generate an implementation like this, and print it to stdout:
package mainimport ("sync/atomic""testing")// GetterMock is a mock implementation of the Getter// interface.typeGetterMockstruct {T*testing.TGetByIDStubfunc(idint) ([]string,error)GetByIDCalledint32GetByNameStubfunc(namestring) ([]string,error)GetByNameCalledint32}// Verify that *GetterMock implements Getter.var_Getter=&GetterMock{}// GetByID is a stub for the Getter.GetByID// method that records the number of times it has been called.func (m*GetterMock)GetByID(idint) ([]string,error) {atomic.AddInt32(&m.GetByIDCalled,1)ifm.GetByIDStub==nil {ifm.T!=nil {m.T.Error("GetByIDStub is nil")}panic("GetByID unimplemented")}returnm.GetByIDStub(id)}// GetByName is a stub for the Getter.GetByName// method that records the number of times it has been called.func (m*GetterMock)GetByName(namestring) ([]string,error) {atomic.AddInt32(&m.GetByNameCalled,1)ifm.GetByNameStub==nil {ifm.T!=nil {m.T.Error("GetByNameStub is nil")}panic("GetByName unimplemented")}returnm.GetByNameStub(name)}
To write the output to a file instead, pass the-w option:mock -w.
Voila! There should now be agetter_mock.go file containing your new mock, inthe same package as the interface definition. Subsequent runs ofmock -w willoverwrite the file, so be careful not to edit it!
Go Generate
Tip
In earlier releases ofmock,go generate was the recommended way to runthe tool. The disadvantage of the approach outlined below is thatgo generate has to runmock once pergo:generate directive, whichrequires parsing the package's AST and computing type information each time.For modules with many mocked interfaces, that extra work adds up. However,mock continues to support this approach for backward compatibility.
To use withgo generate, simply place ago:generate comment somewhere inyour package (e.g. above the interface definition), like so:
//go:generate go tool mock -o getter_mock.go Getter
Note the use of the-o flag, which specifies the output file. If this flag isnot provided, the mocked implementation will be printed to stdout.
Then run thego generate command from the package directory.
About
Automated tool for mocking go interfaces. Works with go:generate.