I get this error in my test; "aborting test due to missing call(s)" using gomock

454 Views Asked by At

Here is my test:

func TestCashDepositAPI(t *testing.T) {
    ....
    testCases := []struct {
        name          string // name of the test scenario - ok, notfound etc
        reqBody       gin.H
        setupAuth     func(t *testing.T, request *http.Request, tokenMaker token.Maker)
        buildStubs    func(store *mockdb.MockStore)
        checkResponse func(t *testing.T, recoder *httptest.ResponseRecorder)
    }{
        {
            name: "OK",
            reqBody: gin.H{
                "AccountNo": account.Accountno,
                "Amount":    amount,
            },
            setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
                .... code omitted ... though this part working
            },
            buildStubs: func(store *mockdb.MockStore) {
                store.EXPECT().GetAccountForUpdate(gomock.Any(), gomock.Eq(account.Accountno)).Times(1).Return(account, nil)

                arg := db.DepositTxParams{
                                        .... code omitted ... though this part working
                    
                }

                store.EXPECT().DepositTx(gomock.Any(), gomock.Eq(arg)).Times(1)

            },
            checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
                require.Equal(t, http.StatusOK, recorder.Code)

            },
        },
    }

    for i := range testCases {
        tc := testCases[i]

        t.Run(tc.name, func(t *testing.T) {
            ctrl := gomock.NewController(t)
            defer ctrl.Finish()

            store := mockdb.NewMockStore(ctrl)
            tc.buildStubs(store)

            server := newTestServer(t, limiter, store, envConfig)
            recorder := httptest.NewRecorder()

            // Marshal reqBody data to JSON
            data, err := json.Marshal(tc.reqBody)
            require.NoError(t, err)

            url := "/sendCashWithdrawal"
            request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
            require.NoError(t, err)

            tc.setupAuth(t, request, server.tokenMaker)
            server.router.ServeHTTP(recorder, request)
            tc.checkResponse(t, recorder)
        })

    }
}

Kindly assist. Been stuck for days. Here is the error in details:

"missing call(s) to *mockdb.MockStore.GetAccountForUpdate()"
"missing call(s) to *mockdb.MockStore.DepositTx()"

Tried everything I know. I see my code is okay but I still get the error.

2

There are 2 best solutions below

0
HuDahai On

The error seems very obvious. In function buildStubs, you expect two functions GetAccountForUpdate and DepositTx to be executed once seperately. But after the request is processed, the two expected function have not been executed.

2
gio On

The error massage is already self explanatory there are missing calls to mock function (GetAccountForUpdate() & DepositTx()) when doing t.run(). So it's aborted even before starting to run test.

Since it's minimal information we can say with this small chunk of code, my suggestion is to check your gin-server url-path ("/sendCashWithdrawal") :

  1. Are you already route your path as needed handle the right function ?
  2. Are this routed handle function implementing 2 expected function ?
  3. Do debug code when running test, see which code produce that error then trace it :).