2

I am trying to mock mysql, but occur error: "because: there are no expected calls of the method "Pod" for that receiver. " I confirmed that I have generated the Pod method with the Mockgen tool, Below is my code

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)
    }
}
Suzijian
  • 21
  • 1
  • 3

1 Answers1

0

You need to include this line in your TestPodService_Create():

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

Adjust the gomock.Any() and .AnyTimes() for your desired goals.