Hi,
I want to mock the functions while i am testing an API , it uses functions of different package but i want to mock them using testify/mock or some other technique in Golang.
Please suggest if we can do this , Is it possible to mock other package functions or not .
API am thinking to do a Unit Testing
func EmailTemplate(c echo.Context) error {
wc := core.GetWebContext(c)
eTplUID := wc.Param("etpl_id")
if len(eTplUID) == 0 {
e := errors.Wrap(nil, "email template uid is required")
wc.Logger().Error(e)
//return e
}
**//Want to mock This function**
u, err := service.GetUserForRequest(wc)
if err != nil {
//return wc.JSON(http.StatusUnauthorized, model.NewErrorResponse("error in fetching user details"))
}
**// Want to mock this function**
appCtx := core.BuildAppContext(context.Background(), wc.Registry)
**// Want to mock this function**
err = service.DeleteEmailTemplate(appCtx, eTplUID, u.OrgID)
if err != nil {
return wc.JSON(http.StatusInternalServerError, model.NewErrorResponse().AddError(err))
}
}
API with which I am planing to test the above API
func TestEmailTemplateDelete(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/test/email_template/")
c.SetParamNames("etpl_id")
c.SetParamValues("12")
if assert.NoError(t, EmailTemplateDelete(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
//assert.Equal(t, userJSON, strings.TrimSpace(rec.Body.String()))
}
}
I am really puzzled to test it, Please suggest the way to test it
Regards
Amit Jain