Reputation: 41
I have a interface, and used mockgen
to create mocks:
type Client {
getBytes(ctx context.Context, token, url string) ([]byte, error)
SendRequest(ctx context.Context, token, id string) (Response, error)
}
type Response {
...
}
I used mockgen
to create the mock file, which looks good. The generated mock file is as this:
// Code generated by MockGen. DO NOT EDIT.
// Source: utils/myservice/client.go
// Package mock_myservice is a generated GoMock package.
package mock_myservice
import (
context "context"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
myservice "github.com/robinhoodmarkets/rh/inbox/utils/myservice"
)
// MockClient is a mock of Client interface.
type MockClient struct {
ctrl *gomock.Controller
recorder *MockClientMockRecorder
}
// MockClientMockRecorder is the mock recorder for MockClient.
type MockClientMockRecorder struct {
mock *MockClient
}
// NewMockClient creates a new mock instance.
func NewMockClient(ctrl *gomock.Controller) *MockClient {
mock := &MockClient{ctrl: ctrl}
mock.recorder = &MockClientMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockClient) EXPECT() *MockClientMockRecorder {
return m.recorder
}
// SendRequest mocks base method.
func (m *MockClient) SendRequest(ctx context.Context, token, id string) (myservice.ToggleSettingsItemResponse, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SendRequest", ctx, token, id)
ret0, _ := ret[0].(myservice.ToggleSettingsItemResponse)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// SendRequest indicates an expected call of SendRequest.
func (mr *MockClientMockRecorder) SendRequest(ctx, token, id interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendRequest", reflect.TypeOf((*MockClient)(nil).SendRequest), ctx, token, id)
}
// getBytes mocks base method.
func (m *MockClient) **getBytes**(ctx context.Context, token, url string) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "getBytes", ctx, token, url)
ret0, _ := ret[0].([]byte)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// getBytes indicates an expected call of getBytes.
func (mr *MockClientMockRecorder) getBytes(ctx, token, url interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "getBytes", reflect.TypeOf((*MockClient)(nil).getBytes), ctx, token, url)
}
However, when I tried to used in unittests, i goet this error:
*MockClient does not implement Client (missing getBytes method): have: mock_myservice.getBytes(context.Context, string, string) ([]byte, error) want: myservice.getBytes(context.Context, string, string) ([]byte, error)
This is wired since the function is indeed in the mocked file.
Upvotes: 2
Views: 1169
Reputation: 41
Actually I figured out myself:
getBytes
is not a public function, as the function names doesn't start with capitalized letter, and hence the implementation is unable to be matched.This is likely a mockgen
issue, as it should give some warning for such usage.
Upvotes: 2