简体   繁体   English

Golang mockgen“未实现”接口问题

[英]Golang mockgen "does not implement" interface issue

I have a interface, and used mockgen to create mocks:我有一个界面,并使用mockgen创建模拟:

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.我使用mockgen创建了mock文件,看起来不错。 The generated mock file is as this:生成的mock文件是这样的:

// 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) *MockClient 没有实现 Client(缺少 getBytes 方法):有:mock_myservice.getBytes(context.Context, string, string) ([]byte, error) 想要:myservice.getBytes(context.Context, string, string) ([]字节,错误)

This is wired since the function is indeed in the mocked file.这是有线的,因为 function 确实在模拟文件中。

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. getBytes不是公共的 function,因为 function 名称不以大写字母开头,因此无法匹配实现。

This is likely a mockgen issue, as it should give some warning for such usage.这可能是一个mockgen问题,因为它应该对这种用法给出一些警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM