federicoleon/golang-examples

testing with interface with struct parameters

pandasagar opened this issue · 0 comments

Hello, I checked your testable code. Its wonderful.
However the mock testing doesn't work if I do a small modification and most of the time the methods uses this approach
and that is just assigning a struct variable and initializing the Interface variable inside the function
Ex: in ping_controller.go

func (controller pingController) Ping(c *gin.Context) {
services.PingService = services.PingServiceImpl {Status: "Hi from Ping"}
result, err := services.PingService.HandlePing()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, result)
}

and in ping_service.go

type pingServiceImpl struct{
Status string
}

func (service pingServiceImpl) HandlePing() (string, error) {
fmt.Println("doing some complex things...")
fmt.Println("status :" service.Status)
return pong, nil
}