登录
首页 >  Golang >  Go问答

Golang mockgen解决未实现接口的问题

来源:stackoverflow

时间:2024-02-08 13:09:22 323浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《Golang mockgen解决未实现接口的问题》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

我有一个界面,并使用 mockgen 来创建模拟:

type client {
    getbytes(ctx context.context, token, url string) ([]byte, error)
    sendrequest(ctx context.context, token, id string) (response, error)
}

type response {
    ...
}

我使用 mockgen 创建模拟文件,看起来不错。生成的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)
}

但是,当我尝试在单元测试中使用时,出现此错误:

*mockclient没有实现client(缺少getbytes方法): 有:mock_myservice.getbytes(context.context, string, string) ([]byte, error) 想要: myservice.getbytes(context.context, string, string) ([]byte, error)

这是有线的,因为该函数确实在模拟文件中。


正确答案


其实我自己也发现了:

  • getBytes 不是公共函数,因为函数名称不是以大写字母开头,因此无法匹配实现。

这可能是 mockgen 问题,因为它应该针对此类使用给出一些警告。

到这里,我们也就讲完了《Golang mockgen解决未实现接口的问题》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>