登录
首页 >  Golang >  Go问答

gomock:因为该接收者未调用预期的“Pod”方法

来源:stackoverflow

时间:2024-02-19 10:21:17 340浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《gomock:因为该接收者未调用预期的“Pod”方法》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

我试图模拟 mysql,但出现错误:“因为:该接收器没有预期调用方法“pod”。” 我确认我已经用mockgen工具生成了pod方法, 下面是我的代码

func TestPodService_Create(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()

    mockFactory := store.NewMockFactory(ctrl)
    mockPod := store.NewMockPodStore(ctrl)
    pods := fake.FakePod(10)
    mockPod.EXPECT().Create(gomock.Eq(context.TODO()), gomock.Eq(pods[0])).Return(nil)
    
    type fields struct {
        store    store.Factory
        redisCli redis.RedisCli
    }
    type args struct {
        ctx context.Context
        pod *model.Pod
    }
    tests := []struct {
        name    string
        fields  fields
        args    args
        wantErr bool
    }{
        // TODO: Add test cases.
        {
            name: "test case 1",
            fields: fields{store: mockFactory,},
            args: args{
                ctx: context.TODO(),
                pod: &pods[0],
            },
            wantErr: false,
        },
    }
    for _, tt := range tests {
        fmt.Printf("begin to test\n")
        podService := &PodService{store: tt.fields.store}
        err := podService.Create(tt.args.ctx, tt.args.pod)
        assert.Equal(t, tt.wantErr, err!=nil)
    }
}

正确答案


您需要在 testpodservice_create() 中包含此行:

mockPod.EXPECT().Pod(gomock.Any()).AnyTimes()

根据您想要的目标调整 gomock.any() 和 .anytimes()。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《gomock:因为该接收者未调用预期的“Pod”方法》文章吧,也可关注golang学习网公众号了解相关技术文章。

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