Skip to content

Latest commit

 

History

History
199 lines (141 loc) · 6.37 KB

File metadata and controls

199 lines (141 loc) · 6.37 KB

gomock

Go Reference Go Report Card codecov

Automatically generates mock code for unit tests from a Go interface (see below for examples.) Generic interfaces are supported.

Status

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.

Installation

$ go install github.com/vibridi/gomock/v3@latest

Project requirements

Go 1.26.2

Usage

Type gomock help for detailed usage tips. Mainly: gomock { help | [options] filename }

In short, it supports the following flags:

  • -f FILE allows 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 FILE if set, tells the program to write the output to FILE. Otherwise it just prints to stdout. You can always capture the output with a pipe. E.g. if you are on MacOS, you could do gomock -f myfile.go | pbcopy
  • -i IDENTIFIER if the input file contains more than one interface declaration, you can use the -i flag to tell the program which one to parse. If not set, the program defaults to the first encountered interface.
  • -x if set, static functions are exported (usually those whose name begins with with and new)
  • -u if set, allows to output default functions and With* functions with unnamed arguments.
  • -d if set, outputs top-level withFunc function 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 is MyService then withFuncDoSomething becomes withFuncMyServiceDoSomething.
  • -p if set, the package name and the service name are merged together, except when the name is explicitly qualified. For example, if the service name is MyService and the package name is foo then NewMockMyService becomes NewMockFooMyService.
  • --local if 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.
  • --struct if set, prints the output in struct style, instead of options style (see below for further details).
  • --name NAME allows to override the interface name used in output types with NAME.
  • --pkgs MAPPING [ --pkgs MAPPING ] maps package names to custom import aliases. MAPPING must in the format 'package=alias'. For example gomock --pkgs foo=foo2 changes foo.Foo from the source file to foo2.Foo.
  • --utype MAPPING [ --utype MAPPING ] allows to manually specify the underlying type of a named type. If the --pkgs option is specified, the MAPPING's keys must be the aliased package name. For example gomock --pkgs foo=foo2 --utype foo2.Foo=int
  • --help, -h prints a help message.
  • --version, -v prints the version number.

Breaking changes from version 2.x

  • The option -q is removed. It's assumed that mocked types are always qualified with their package name. The option --local can be used instead to opt out (i.e. to not qualify them).
  • The option --struct-type has been renamed to --struct. It has the same effect.

Features

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.

Examples (options style)

You can try out the tool on any of your interfaces. The _examples folder in this repository contains a few interface types usefult to try out the tool:

$ gomock -f _example/_example.go

The _example.go file declares a type TestInterface interface { ... }. The default options-style mock code generated by gomock looks like this:

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,
	}
}

That can be used in unit tests as such:

myMock := newMockTestInterface(
    withFuncGet(f func() string {
        return "test-value"
    }),
)
// myMock.Get() returns "test-value"

// inject mock into constructor that expects a TestInterface
obj := NewObject(myMock)

Examples (struct style)

Running gomock with the --struct option generates the mock code in struct style:

$ gomock -f _example/_example.go --struct

This 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)
	}
}

That can be used in unit tests as such:

myMock := mockTestInterface{
    GetFunc: func() string {
        return "test-value"
    },
}
// myMock.Get() returns "test-value"

// inject mock into constructor that expects a TestInterface
obj := NewObject(myMock)

Authors

Currently there are no other contributors

License

This project is licensed under the MIT License - see the LICENSE.md file for details