Github.com/uber/mock: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 8: Line 8:
=Installation=
=Installation=


To install <code>mockgen</code>:
==<tt>mockgen</tt> Installation==
{{External|https://github.com/uber-go/mock#installation}}


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
go install go.uber.org/mock/mockgen@latest
</syntaxhighlight>


<syntaxhighlight lang='bash'>
mockgen -version
v0.4.0
</syntaxhighlight>
</syntaxhighlight>



Revision as of 21:10, 28 August 2024

External

Internal

Overview

Installation

mockgen Installation

https://github.com/uber-go/mock#installation
go install go.uber.org/mock/mockgen@latest
mockgen -version
v0.4.0

Import

import "go.uber.org/mock/gomock"

mockgen

To generate the mock code at stdout:

mockgen -package <name_of_the_package_mock_will_belong_to> <package_import_path> <InterfaceName>[,<InterfaceName>]

Bazel Integration with the gomock Rule

https://github.com/jmhodges/bazel_gomock#use

Attributes

name

interfaces

package

The package name to use in the generated output. See the gomock documentation on -package for more information.

library

The go_library to find the interfaces in.

out

The name of the generated source code file. The file will be written in the current directory.

Programming Model

Setup Expectations

Define the Behavior of a Method

Configuring a mock to handle a specific method call.

someMock.EXPECT().SomeMethod(gomock.Any(), "some specific value").Return("some other value")

Note that if the method is not called at the end of the test, the mock will fail the test with:

missing call(s) to *mock_pkg.MockSomething.SomeMethod(...)
aborting test due to missing call(s)