samedi 21 juillet 2018

How to unit test a go function with internal function calls

How do I test the below function? Everything is fine till the last return statement. I don't know how to mock the getResponse() function.

func Chatbot(c echo.Context, payload payload.Handler) error {   
    err := c.Bind(payload)  
    if err != nil {         
        return c.JSON(http.StatusOK, map[string]string{"text": "invalid payload"})  
    }   

    if !payload.Validate() {        
        return c.String(http.StatusForbidden, "Not allowed")    
    }   

    log.Printf("chatbot - %s - %s", payload.GetSenderName(), payload.GetMessage())  
    return c.JSON(http.StatusOK, getResponse(ctx, payload)) 
}

getResponse() is an internal function which is not exposed.

What is the correct way to write such code so that it is easier to test.

Aucun commentaire:

Enregistrer un commentaire