Need some direct anwsers which I wont get out of youtube tutorials. This is my controller layer and I wanna do the unit-testing.
1.Question: Do I need to test or only to mock the req.Query() "share, number, singlePrice" and if yes, how do I do this? 2.Question: If I have a function, do I have to test only the input and output values or everything that happens in the function? 3.Question: Could somebody of you give me a full example of how to test this method "CalculateEarning()"?
With LOVE Flaxi
func (stockAPI *StockController) CalculateEarning(req *gin.Context) {
share := req.Query("share")
number := req.Query("number")
singlePrice := req.Query("singlePrice")
if share == "" || number == "" || singlePrice == "" {
req.JSON(http.StatusBadRequest, "bad request")
return
} else {
var responseEntity dto.OutputDTO
var stockError *customError.ErrorStock
responseEntity.Share = share
responseEntity.Numbers, _ = strconv.ParseFloat(strings.Replace(number, ",", ".", 1), 64)
responseEntity.Price, _ = strconv.ParseFloat(strings.Replace(singlePrice, ",", ".", 1), 64)
key := req.GetHeader("x-rapidapi-key")
ret, err := stockAPI.stockService.CalculateEarning(key, responseEntity)
if errors.As(err, &stockError) {
if stockError.Code == 200 {
req.JSON(http.StatusOK, ret)
} else if stockError.Code == 204 {
req.JSON(http.StatusNoContent, stockError.Text)
} else if stockError.Code == 401 {
req.JSON(http.StatusUnauthorized, stockError.Text)
} else if stockError.Code == 403 {
req.JSON(http.StatusForbidden, stockError.Text)
} else if stockError.Code == 500 {
req.JSON(http.StatusInternalServerError, stockError.Text)
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire