Search code examples
gogo-gin

How to test Gin-gonic controller properly?


I'm learning golang by using Gin-gonic as http handler framework. I have a controller for an endpoint which makes operations with my own Email struct as following:

func EmailUserVerification(c *gin.Context) {
    var input validators.EmailUserVerificationValidator
    if err := c.ShouldBindJSON(&input); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    email := models.Email{
        To:       input.To,
        Subject:  input.Subject,
        Template: "user_verification.html",
        TemplateData: notices.EmailUserVerificationTemplateData{
            Name:             input.Name,
            VerificationLink: input.VerificationLink,
        },
        Sender: models.NewEmailSMTPSender(input.From),
    }

    if err := email.Deliver(); err != nil {
        panic(err)
    }
    c.JSON(http.StatusCreated, nil)
}

Struct Email is already test, nevertheless I do not know how to test this method properly. how it is supposed Email struct to be mocked here?

I registered the handler as gin-gonic documentation say:

router.POST("/emails/users/verify", controllers.EmailUserVerification)

Maybe could I inject an Email interface in the handler? if it is the case, how can I inject it?

Thanks in advance ^^


Solution

  • You can testing it by creating test file that have testing function inside it and mock other calling function. For me I use testify/mock to mocking func (for further explanation, you should reading from other site first like medium and GitHub mock repo )

    For example If I have route path like this v1.POST("/operators/staffs", handler.CreateStaff) and have function handler.CreateStaff that inside call func handler.OperatorStaffUseCase.CreateStaff

    I will create file create_staff_test.go that should look like this

    package operator_staff
    
    import (
        "encoding/json"
        "fmt"
        "github.com/gin-gonic/gin"
        "github.com/golang/mock/gomock"
        jsoniter "github.com/json-iterator/go"
        "github.com/pkg/errors"
        "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/mock"
        "net/http"
        "net/http/httptest"
        "onboarding-service/app/entities"
        staffUseCaseMock "onboarding-service/app/mocks/usecases"
        "strings"
        "testing"
    )
    
    func TestStaffHandler_CreateStaff(t *testing.T) {
        var (
            invitationId   = "3da465a6-be13-405e-a653-c68adf59f2be"
            firstName      = "Tom"
            lastName       = "Sudchai"
            roleId         = uint(1)
            roleName       = "role"
            operatorCode   = "velo"
            email          = "toms@gmail.com"
            password       = "P@ssw0rd"
            hashedPassword = "$2y$12$S0Gbs0Qm5rJGibfFBTARa.6ap9OBuXYbYJ.deCzsOo4uQNJR1KbJO"
        )
    
        gin.SetMode(gin.TestMode)
        ctrl := gomock.NewController(t)
        defer ctrl.Finish()
        staffMock := staffUseCaseMock.NewMockOperatorStaffUseCase(ctrl)
    
        executeWithContext := func(mockUseCase *staffUseCaseMock.MockOperatorStaffUseCase, jsonRequestBody []byte, operatorCode string) *httptest.ResponseRecorder {
            response := httptest.NewRecorder()
            context, ginEngine := gin.CreateTestContext(response)
    
            requestUrl := "/v1/operators/staffs"
            httpRequest, _ := http.NewRequest("POST", requestUrl, strings.NewReader(string(jsonRequestBody)))
    
            NewEndpointHTTPHandler(ginEngine, mockUseCase)
            ginEngine.ServeHTTP(response, httpRequest)
            return response
        }
    
        createdStaffEntity := entities.OperatorStaff{
            ID:        roleId,
            FirstName: firstName,
            LastName:  lastName,
            Email:     email,
            Password:  hashedPassword,
            Operators: []entities.StaffOperator{{
                OperatorCode: operatorCode, RoleID: roleId,
            }},
        }
    
        t.Run("Happy", func(t *testing.T) {
            jsonRequestBody, _ := json.Marshal(createStaffFromInviteRequestJSON{
                InvitationId:    invitationId,
                FirstName:       firstName,
                LastName:        lastName,
                Password:        password,
                ConfirmPassword: password,
            })
        
            staffMock.EXPECT().CreateStaff(gomock.Any(), gomock.Any()).Return(&createdStaffEntity, nil)
    
            res := executeWithContext(staffMock, jsonRequestBody, operatorCode)
            assert.Equal(t, http.StatusOK, res.Code)
        })
    }
    
    

    you will see first mocking func when initially test staffMock := staffUseCaseMock.NewMockOperatorStaffUseCase(ctrl) and call in inside test case happy case

    staffMock.EXPECT().CreateStaff(gomock.Any(), gomock.Any()).Return(&createdStaffEntity, nil) that is mocking function that will return value as I want (again you should read more about gomock and you will understand what I trying to say)

    or for easy way to learn about testing is fallow this tutorial https://github.com/JacobSNGoodwin/memrizr