【问题标题】:gomock: because: there are no expected calls of the method "Pod" for that receivergomock:因为:该接收器没有预期的方法 \"Pod\" 调用
【发布时间】:2022-09-27 22:42:34
【问题描述】:

我正在尝试模拟 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)
    }
}

    标签: go gomock


    【解决方案1】:

    您需要在 TestPodService_Create() 中包含这一行:

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

    调整 gomock.Any() 和 .AnyTimes() 以达到您想要的目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-03
      • 2021-10-03
      • 2016-01-09
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多