Github.com/uber/mock: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 7: Line 7:


=Installation=
=Installation=
==<tt>mockgen</tt> Installation==
{{External|https://github.com/uber-go/mock#installation}}
<syntaxhighlight lang='bash'>
go install go.uber.org/mock/mockgen@latest
</syntaxhighlight>
<syntaxhighlight lang='bash'>
mockgen -version
v0.4.0
</syntaxhighlight>
=Import=
<syntaxhighlight lang='go'>
import "go.uber.org/mock/gomock"
</syntaxhighlight>


=<tt>mockgen</tt>=
=<tt>mockgen</tt>=
Method 1 ("Package"):


To generate the mock code at stdout:
To generate the mock code at stdout:
Line 14: Line 33:
mockgen -package <name_of_the_package_mock_will_belong_to> <package_import_path> <InterfaceName>[,<InterfaceName>]
mockgen -package <name_of_the_package_mock_will_belong_to> <package_import_path> <InterfaceName>[,<InterfaceName>]
</syntaxhighlight>
</syntaxhighlight>
Once the source file is generated, you may need to replace <code>gomock "github.com/golang/mock/gomock"</code> with <code>go.uber.org/mock/gomock</code>.
Method 2 ("Source Mode"):
Navigate to the directory that contains the source code and:
<syntaxhighlight lang='bash'>
mockgen -package <name_of_the_package_mock_will_belong_to> -source=myfile.go [InterfaceName]
</syntaxhighlight>
=Bazel Integration with the <code>gomock</code> Rule=
{{External|https://github.com/jmhodges/bazel_gomock#use}}
==Attributes==
===<tt>name</tt>===
===<tt>interfaces</tt>===
===<tt>package</tt>===
The package name to use in the generated output. See the <code>gomock</code> documentation on <code>-package</code> for more information.
===<tt>library</tt>===
The <code>go_library</code> to find the interfaces in.
===<tt>out</tt>===
The name of the generated source code file. The file will be written in the current directory.


=Programming Model=
=Programming Model=
==Instantiate the Mock==
<syntaxhighlight lang='go'>
import "go.uber.org/mock/gomock"
...
func TestSomething(t *testing.T) {
  mockController := gomock.NewController(t)
  mockSomething := mockgeneratedpkg.NewMockSomething(mockController)
  ...
}
</syntaxhighlight>
==Setup Expectations==
==Setup Expectations==


Line 23: Line 77:


<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
someMock.EXPECT().SomeMethod(gomock.Any(), "some specific value").Return("some other value")
mockSomething.EXPECT().SomeMethod(gomock.Any(), "some specific value").Return("some other value")
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 21:31, 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

Method 1 ("Package"):

To generate the mock code at stdout:

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

Once the source file is generated, you may need to replace gomock "github.com/golang/mock/gomock" with go.uber.org/mock/gomock.

Method 2 ("Source Mode"):

Navigate to the directory that contains the source code and:

mockgen -package <name_of_the_package_mock_will_belong_to> -source=myfile.go [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

Instantiate the Mock

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

...

func TestSomething(t *testing.T) {
  mockController := gomock.NewController(t)
  mockSomething := mockgeneratedpkg.NewMockSomething(mockController)
  ...
}

Setup Expectations

Define the Behavior of a Method

Configuring a mock to handle a specific method call.

mockSomething.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)