Mocking

The concept of mocking is very popular in the field of unit testing software. It's best described via an example. Let's say we would like to unit test one of the HTTP handler functions of the GoMusic application. The GetProducts() method is a good method to showcase our example, since the purpose of this method was to return a list of all of the products that are available for sale in our GoMusic store. Here is what the code for GetProducts() looked like:

func (h *Handler) GetProducts(c *gin.Context) {  if h.db == nil {    c.JSON(http.StatusInternalServerError, gin.H{"error": "server database error"})    return  }  products, err := h.db.GetAllProducts()  if err != nil {    c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return ...

Get Hands-On Full Stack Development with Go now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.