1
- gomock [ ![ Build Status] [ travis-ci-badge ]] [ travis-ci ] [ ![ GoDoc] [ godoc-badge ]] [ godoc ]
2
- ======
1
+ # gomock
3
2
4
- GoMock is a mocking framework for the [ Go programming language] [ golang ] . It
3
+ [ ![ Build Status] [ ci-badge ]] [ ci-runs ] [ ![ Go Reference] [ reference-badge ]] [ reference ]
4
+
5
+ gomock is a mocking framework for the [ Go programming language] [ golang ] . It
5
6
integrates well with Go's built-in ` testing ` package, but can be used in other
6
7
contexts too.
7
8
8
- Installation
9
- ------------
9
+ ## Installation
10
10
11
11
Once you have [ installed Go] [ golang-install ] , install the ` mockgen ` tool.
12
12
@@ -19,9 +19,7 @@ GO111MODULE=on go get github.com/golang/mock/mockgen@v1.5.0
19
19
If you use ` mockgen ` in your CI pipeline, it may be more appropriate to fixate
20
20
on a specific mockgen version.
21
21
22
-
23
- Documentation
24
- -------------
22
+ ## Documentation
25
23
26
24
After installing, you can use ` go doc ` to get documentation:
27
25
@@ -30,14 +28,14 @@ go doc github.com/golang/mock/gomock
30
28
```
31
29
32
30
Alternatively, there is an online reference for the package hosted on GoPkgDoc
33
- [ here] [ gomock-ref ] .
31
+ [ here] [ gomock-reference ] .
34
32
35
- Running mockgen
36
- ---------------
33
+ ## Running mockgen
37
34
38
35
` mockgen ` has two modes of operation: source and reflect.
39
36
40
- #### Source mode
37
+ ### Source mode
38
+
41
39
Source mode generates mock interfaces from a source file.
42
40
It is enabled by using the -source flag. Other flags that
43
41
may be useful in this mode are -imports and -aux_files.
@@ -48,7 +46,8 @@ Example:
48
46
mockgen -source=foo.go [other options]
49
47
```
50
48
51
- #### Reflect mode
49
+ ### Reflect mode
50
+
52
51
Reflect mode generates mock interfaces by building a program
53
52
that uses reflection to understand interfaces. It is enabled
54
53
by passing two non-flag arguments: an import path, and a
@@ -65,54 +64,55 @@ mockgen database/sql/driver Conn,Driver
65
64
mockgen . Conn,Driver
66
65
```
67
66
68
- #### Flags
67
+ ### Flags
68
+
69
69
The ` mockgen ` command is used to generate source code for a mock
70
70
class given a Go source file containing interfaces to be mocked.
71
71
It supports the following flags:
72
72
73
- * ` -source ` : A file containing interfaces to be mocked.
73
+ - ` -source ` : A file containing interfaces to be mocked.
74
+
75
+ - ` -destination ` : A file to which to write the resulting source code. If you
76
+ don't set this, the code is printed to standard output.
74
77
75
- * ` -destination ` : A file to which to write the resulting source code. If you
76
- don't set this, the code is printed to standard output.
78
+ - ` -package ` : The package to use for the resulting mock class
79
+ source code. If you don't set this, the package name is ` mock_ ` concatenated
80
+ with the package of the input file.
77
81
78
- * ` -package ` : The package to use for the resulting mock class
79
- source code. If you don't set this, the package name is ` mock_ ` concatenated
80
- with the package of the input file.
82
+ - ` -imports ` : A list of explicit imports that should be used in the resulting
83
+ source code, specified as a comma-separated list of elements of the form
84
+ ` foo=bar/baz ` , where ` bar/baz ` is the package being imported and ` foo ` is
85
+ the identifier to use for the package in the generated source code.
81
86
82
- * ` -imports ` : A list of explicit imports that should be used in the resulting
83
- source code, specified as a comma-separated list of elements of the form
84
- ` foo=bar/baz ` , where ` bar/baz ` is the package being imported and ` foo ` is
85
- the identifier to use for the package in the generated source code.
87
+ - ` -aux_files ` : A list of additional files that should be consulted to
88
+ resolve e.g. embedded interfaces defined in a different file. This is
89
+ specified as a comma-separated list of elements of the form
90
+ ` foo=bar/baz.go ` , where ` bar/baz.go ` is the source file and ` foo ` is the
91
+ package name of that file used by the -source file.
86
92
87
- * ` -aux_files ` : A list of additional files that should be consulted to
88
- resolve e.g. embedded interfaces defined in a different file. This is
89
- specified as a comma-separated list of elements of the form
90
- ` foo=bar/baz.go ` , where ` bar/baz.go ` is the source file and ` foo ` is the
91
- package name of that file used by the -source file.
93
+ - ` -build_flags ` : (reflect mode only) Flags passed verbatim to ` go build ` .
92
94
93
- * ` -build_flags ` : (reflect mode only) Flags passed verbatim to ` go build ` .
95
+ - ` -mock_names ` : A list of custom names for generated mocks. This is specified
96
+ as a comma-separated list of elements of the form
97
+ ` Repository=MockSensorRepository,Endpoint=MockSensorEndpoint ` , where
98
+ ` Repository ` is the interface name and ` MockSensorRepository ` is the desired
99
+ mock name (mock factory method and mock recorder will be named after the mock).
100
+ If one of the interfaces has no custom name specified, then default naming
101
+ convention will be used.
94
102
95
- * ` -mock_names ` : A list of custom names for generated mocks. This is specified
96
- as a comma-separated list of elements of the form
97
- ` Repository=MockSensorRepository,Endpoint=MockSensorEndpoint ` , where
98
- ` Repository ` is the interface name and ` MockSensorRepository ` is the desired
99
- mock name (mock factory method and mock recorder will be named after the mock).
100
- If one of the interfaces has no custom name specified, then default naming
101
- convention will be used.
102
-
103
- * ` -self_package ` : The full package import path for the generated code. The purpose
104
- of this flag is to prevent import cycles in the generated code by trying to include
105
- its own package. This can happen if the mock's package is set to one of its
106
- inputs (usually the main one) and the output is stdio so mockgen cannot detect the
107
- final output package. Setting this flag will then tell mockgen which import to exclude.
103
+ - ` -self_package ` : The full package import path for the generated code. The
104
+ purpose of this flag is to prevent import cycles in the generated code by
105
+ trying to include its own package. This can happen if the mock's package is
106
+ set to one of its inputs (usually the main one) and the output is stdio so
107
+ mockgen cannot detect the final output package. Setting this flag will then
108
+ tell mockgen which import to exclude.
108
109
109
- * ` -copyright_file ` : Copyright file used to add copyright header to the resulting source code.
110
+ - ` -copyright_file ` : Copyright file used to add copyright header to the resulting source code.
110
111
111
112
For an example of the use of ` mockgen ` , see the ` sample/ ` directory. In simple
112
113
cases, you will need only the ` -source ` flag.
113
114
114
- Building Mocks
115
- --------------
115
+ ## Building Mocks
116
116
117
117
``` go
118
118
type Foo interface {
@@ -150,8 +150,7 @@ passing a *testing.T into `gomock.NewController(t)` you no longer need to call
150
150
` ctrl.Finish() ` explicitly. It will be called for you automatically from a self
151
151
registered [ Cleanup] ( https://pkg.go.dev/testing?tab=doc#T.Cleanup ) function.
152
152
153
- Building Stubs
154
- --------------
153
+ ## Building Stubs
155
154
156
155
``` go
157
156
type Foo interface {
@@ -193,20 +192,20 @@ func TestFoo(t *testing.T) {
193
192
}
194
193
```
195
194
196
- ### Modifying Failure Messages
195
+ ## Modifying Failure Messages
197
196
198
197
When a matcher reports a failure, it prints the received (` Got ` ) vs the
199
198
expected (` Want ` ) value.
200
199
201
- ```
200
+ ``` shell
202
201
Got: [3]
203
202
Want: is equal to 2
204
203
Expected call at user_test.go:33 doesn' t match the argument at index 1.
205
204
Got: [0 1 1 2 3]
206
205
Want: is equal to 1
207
206
```
208
207
209
- ##### Modifying ` Want `
208
+ ### Modifying `Want`
210
209
211
210
The `Want` value comes from the matcher' s `String ()` method. If the matcher' s
212
211
default output doesn' t meet your needs, then it can be modified as follows:
@@ -221,7 +220,7 @@ gomock.WantFormatter(
221
220
This modifies the ` gomock.Eq(15)` matcher' s output for `Want:` from `is equal
222
221
to 15` to `is equal to fifteen`.
223
222
224
- ##### Modifying ` Got `
223
+ ### Modifying `Got`
225
224
226
225
The `Got` value comes from the object' s ` String()` method if it is available.
227
226
In some cases the output of an object is difficult to read (e.g., ` []byte` ) and
@@ -240,10 +239,10 @@ gomock.GotFormatterAdapter(
240
239
241
240
If the received value is ` 3` , then it will be printed as ` 03` .
242
241
243
- [ golang ] : http://golang.org/
244
- [ golang-install ] : http://golang.org/doc/install.html#releases
245
- [ gomock-ref ] : http ://godoc.org /github.com/golang/mock/gomock
246
- [ travis- ci-badge] : https://travis-ci.org /golang/mock. svg?branch=master
247
- [ travis-ci ] : https://travis-ci.org /golang/mock
248
- [ godoc -badge] : https://godoc.org/ github.com/golang/mock/gomock?status .svg
249
- [ godoc ] : https://godoc.org /github.com/golang/mock/gomock
242
+ [golang]: http://golang.org/
243
+ [golang-install]: http://golang.org/doc/install.html#releases
244
+ [gomock-reference ]: https ://pkg.go.dev /github.com/golang/mock/gomock
245
+ [ci-badge]: https://github.com /golang/mock/actions/workflows/test.yml/badge. svg
246
+ [ci-runs ]: https://github.com /golang/mock/actions
247
+ [reference -badge]: https://pkg.go.dev/badge/ github.com/golang/mock.svg
248
+ [reference ]: https://pkg.go.dev /github.com/golang/mock
0 commit comments