|
| 1 | +--- |
| 2 | +title: gomock |
| 3 | +--- |
| 4 | + |
| 5 | +`gomock` mocks are derived from the project at https://github.com/uber-go/mock. |
| 6 | + |
| 7 | +## Description |
| 8 | + |
| 9 | +=== "Interface" |
| 10 | + |
| 11 | + ```go |
| 12 | + package test |
| 13 | + |
| 14 | + type Requester interface { |
| 15 | + Get(path string) (string, error) |
| 16 | + } |
| 17 | + ``` |
| 18 | + |
| 19 | + |
| 20 | +=== "Example Usage" |
| 21 | + |
| 22 | + ```go |
| 23 | + package test |
| 24 | + |
| 25 | + import ( |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + ) |
| 30 | + |
| 31 | + func TestRequesterGoMock(t *testing.T) { |
| 32 | + ctrl := gomock.NewController(t) |
| 33 | + m := NewGoMockRequester(ctrl) |
| 34 | + m.EXPECT().Get("foo").Return("bar", nil).Times(1) |
| 35 | + retString, err := m.Get("foo") |
| 36 | + assert.NoError(t, err) |
| 37 | + assert.Equal(t, "bar", retString) |
| 38 | + } |
| 39 | + ``` |
| 40 | + |
| 41 | +=== "`.mockery.yml`" |
| 42 | + |
| 43 | + ```yaml |
| 44 | + template: gomock |
| 45 | + template-data: |
| 46 | + typed: true |
| 47 | + packages: |
| 48 | + github.com/vektra/mockery/v3/pkg/fixtures: |
| 49 | + config: |
| 50 | + dir: "{{.InterfaceDir}}" |
| 51 | + filename: "gomocks.go" |
| 52 | + pkgname: "test" |
| 53 | + structname: "GoMock{{.InterfaceName}}" |
| 54 | + interfaces: |
| 55 | + Requester: |
| 56 | + ``` |
| 57 | + |
| 58 | +=== "`mocks_gomock.go`" |
| 59 | + |
| 60 | + ```go |
| 61 | + // Code generated by mockery; DO NOT EDIT. |
| 62 | + // github.com/vektra/mockery |
| 63 | + // template: gomock |
| 64 | + // source: github.com/vektra/mockery/v3/internal/fixtures (interfaces: Requester) |
| 65 | + |
| 66 | + // Package test is a generated GoMock package. |
| 67 | + package test |
| 68 | + |
| 69 | + import ( |
| 70 | + "reflect" |
| 71 | + |
| 72 | + "go.uber.org/mock/gomock" |
| 73 | + ) |
| 74 | + |
| 75 | + // GoMockRequester is a mock of Requester interface. |
| 76 | + type GoMockRequester struct { |
| 77 | + ctrl *gomock.Controller |
| 78 | + recorder *GoMockRequesterMockRecorder |
| 79 | + isgomock struct{} |
| 80 | + } |
| 81 | + |
| 82 | + // GoMockRequesterMockRecorder is the mock recorder for GoMockRequester. |
| 83 | + type GoMockRequesterMockRecorder struct { |
| 84 | + mock *GoMockRequester |
| 85 | + } |
| 86 | + |
| 87 | + // NewGoMockRequester creates a new mock instance. |
| 88 | + func NewGoMockRequester(ctrl *gomock.Controller) *GoMockRequester { |
| 89 | + mock := &GoMockRequester{ctrl: ctrl} |
| 90 | + mock.recorder = &GoMockRequesterMockRecorder{mock} |
| 91 | + return mock |
| 92 | + } |
| 93 | + |
| 94 | + // EXPECT returns an object that allows the caller to indicate expected use. |
| 95 | + func (m *GoMockRequester) EXPECT() *GoMockRequesterMockRecorder { |
| 96 | + return m.recorder |
| 97 | + } |
| 98 | + |
| 99 | + // Get mocks base method. |
| 100 | + func (m *GoMockRequester) Get(path string) (string, error) { |
| 101 | + // ... |
| 102 | + } |
| 103 | + |
| 104 | + // Get indicates an expected call of Get. |
| 105 | + func (mr *GoMockRequesterMockRecorder) Get(path any) *GoMockRequesterGetCall { |
| 106 | + // ... |
| 107 | + } |
| 108 | + |
| 109 | + // GoMockRequesterGetCall wrap *gomock.Call |
| 110 | + type GoMockRequesterGetCall struct { |
| 111 | + *gomock.Call |
| 112 | + } |
| 113 | + |
| 114 | + // Return rewrite *gomock.Call.Return |
| 115 | + func (c *GoMockRequesterGetCall) Return(s string, err error) *GoMockRequesterGetCall { |
| 116 | + c.Call = c.Call.Return(s, err) |
| 117 | + return c |
| 118 | + } |
| 119 | + |
| 120 | + // Do rewrite *gomock.Call.Do |
| 121 | + func (c *GoMockRequesterGetCall) Do(f func(string) (string, error)) *GoMockRequesterGetCall { |
| 122 | + c.Call = c.Call.Do(f) |
| 123 | + return c |
| 124 | + } |
| 125 | + |
| 126 | + // DoAndReturn rewrite *gomock.Call.DoAndReturn |
| 127 | + func (c *GoMockRequesterGetCall) DoAndReturn(f func(string) (string, error)) *GoMockRequesterGetCall { |
| 128 | + c.Call = c.Call.DoAndReturn(f) |
| 129 | + return c |
| 130 | + } |
| 131 | + |
| 132 | + ``` |
| 133 | + |
| 134 | + |
| 135 | +## `template-data` |
| 136 | + |
| 137 | +`gomock` accepts the following `#!yaml template-data:` keys: |
| 138 | + |
| 139 | +| key | type | description | |
| 140 | +|-----|------|-------------| |
| 141 | +| `boilerplate-file` | `#!yaml string` | Specify a path to a file that contains comments you want displayed at the top of all generated mock files. This is commonly used to display license headers at the top of your source code. | |
| 142 | +| `mock-build-tags` | `#!yaml string` | Set the build tags of the generated mocks. Read more about the [format](https://pkg.go.dev/cmd/go#hdr-Build_constraints). | |
| 143 | +| `no-source-comment` | `#!yaml bool` | Disable writing the original package and interface names in a comment. | |
| 144 | +| `no-pkg-comment` | `#!yaml bool` | Disable writing a package documentation comment (godoc). | |
| 145 | +| `typed` | `#!yaml bool` | Generate type-safe 'Return', 'Do', 'DoAndReturn' functions. | |
| 146 | + |
| 147 | + |
| 148 | +### Schema |
| 149 | + |
| 150 | +```json |
| 151 | +--8<-- "internal/mock_gomock.templ.schema.json" |
| 152 | +``` |
0 commit comments