Golang mockgen “does not implement” interface issue Golang的mockgen“未实现”接口问题

huangapple go评论96阅读模式
英文:

Golang mockgen "does not implement" interface issue

问题

我有一个接口,并使用mockgen创建了模拟对象:

type Client interface {
    getBytes(ctx context.Context, token, url string) ([]byte, error)
    SendRequest(ctx context.Context, token, id string) (Response, error)
}

type Response struct {
    ...
}

我使用mockgen创建了模拟文件,看起来很好。生成的模拟文件如下所示:

// 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)
}

然而,当我尝试在单元测试中使用时,出现了以下错误:

> *MockClient未实现Client接口(缺少getBytes方法):
> 已有:mock_myservice.getBytes(context.Context, string, string) ([]byte, error)
> 期望:myservice.getBytes(context.Context, string, string) ([]byte, error)

这很奇怪,因为该函数确实在模拟文件中。

英文:

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.

答案1

得分: 2

实际上,我自己找到了答案:

  • getBytes 不是一个公共函数,因为函数名没有以大写字母开头,所以无法匹配到实现。

这很可能是 mockgen 的问题,因为它应该对这种用法给出一些警告。

英文:

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.

huangapple
  • 本文由 发表于 2022年8月29日 01:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/73520671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定