1

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.

Matteo
  • 37,680
  • 11
  • 100
  • 115
ramshadows
  • 11
  • 1

2 Answers2

0

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.

HuDahai
  • 47
  • 4
0

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 :).
gio
  • 1
  • 2
  • Hello gio. Thanks for the insight. The error is self explanatory, true. The route path handles the right function. The handler function implements the two function. The GetAccountForUpdate retrieves the request accouunt from db and does some validation. The DepositTx function does the actual deposit process. From the debug breakpoints, it is true t.run() is aborted when it gets to store.Expect.GetAccountForUpdate but I can't figure out how to work around the issue. – ramshadows Jun 15 '23 at 07:13
  • hi ramshadows since all things are satisfied as expected and still produce some error, maybe there are something's missed in the code (example: wrong import, re-run mock generate, etc ) i hope you found the answer. do debug start from finding the function that making that error in mock function then trace it up cheers. – gio Jun 15 '23 at 18:28