Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record argument values called to a mock function

I am trying to get the called argument values to a mock function. Does Go mock have a functionality similar to someMockFunction.mock.calls[0][0]).toBe('first arg') in Jest or ArgumentCaptor in Mockito?

Here's my use case.

I have a Client struct that calls an external API.

func (c *Client) SubmitForm(ctx context.Context ) (string, error) {
     formVals := url.Values{}
     // Payload created here

     apiUrl := url.URL{Scheme: "http", Host: "api.mytestservice.com, Path: "/submit"}

     httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiUrl.String(), strings.NewReader(formVals.Encode()))
     httpReq.Header.Set("Authorization", <sometoken>)
     httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")

     resp, err := c.httpClient.Do(ctx, submitPickupSchedule, httpReq) // This calls to a mock httpClient.Do()

     // error handling and return values goes here
     return resp, err
}

And my mocks created with Mockery (I tried Mockgen as well. )

mockHTTPClient := mock_httputils.NewMockHTTPClient(ctrl)
client = Client{httpClient: mockHTTPClient} // Using the mock HTTP client here

t.Run("should call the Do with post request successfully", func(t *testing.T) {
    ctx := context.Background()
    ctx = context.WithValue(ctx, utils.CTXAuthTokenKey, "value")

    mockHTTPClient.EXPECT().Do(ctx, "SubmitCall",
        gomock.Any()).Return(&http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewReader([]byte("SUCCESS")))}, nil)

    resp, err := client.SubmitForm(ctx)
    // assertions here and everything works as expected. It calls the mock method.
}

After calling the mock Do() I am trying to get the actual arguments that got called into this function. i.e., I want to inspect the req object that was created within the SubmitForm method and passed into this mock Do().

Is there a way in GoLang to do this?

like image 398
Klaus Avatar asked Nov 23 '25 15:11

Klaus


1 Answers

Following @mkopriva's comment, I was able to capture arguments in a mock function. Posting my solution here so it might help anyone in the future.

 func TestArgumentCaptor(t *testing.T){
        var req *http.Request // To record the request attributes

        // Original HTTPClient Do(ctx context.Context, name string, req *http.Request) (resp *http.Response, err error) has this signature
        mockHTTPClient.EXPECT().Do(ctx, "Submit", gomock.Any()).DoAndReturn(
            // signature of the anonymous function must have the same method signature as the original method
            func(argCtx context.Context, argName string, argReq *http.Request) (resp *http.Response, err error) {
                req = argReq
                return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewReader([]byte("SUCCESS")))}, nil
            })

        mockHTTPClient.DoCall() // Calls the mockHTTPClient.Do() method.

        // URI check
        assert.NotNil(t, req)
        assert.Equal(t, req.URL.Scheme, <expected-scheme>, "URL scheme mismatch")
        assert.Equal(t, req.URL.Host, <expected-hist>, "Host mismatch")
        assert.Equal(t, req.URL.Path, <expected-path>, "Path mismatch")

        // Headers check
        assert.Equal(t, req.Header.Get("Authorization"),<expected-header>)
        assert.Equal(t, req.Header.Get("Content-Type"), <expected-header>)

 }
like image 178
Klaus Avatar answered Nov 25 '25 11:11

Klaus