如何测试使用中间件的处理程序我正在尝试对使用中间件但不作为依赖项的处理程序进行单元测试。我的处理程序代码如下所示:package handlersimport ( "github.com/gin-gonic/gin" "github.com/google/uuid")type Handler interface{ FindById(c *gin.Context)}type handler struct{}func (*handler) FindById(context *gin.Context) { id := context.MustGet("id").(uuid.UUID) // do something with `id`...}中间件的代码:package middlewaresimport ( "net/http" "github.com/gin-gonic/gin" "github.com/google/uuid")func Id(context *gin.Context) { id, err := uuid.Parse(context.Param("id")) if err != nil { context.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "errors": []string{"id is not valid UUID"} }) return } context.Set("id", id)}我该如何模拟:id := context.MustGet("id").(uuid.UUID)测试handler结构?
- 1 回答
- 0 关注
- 106 浏览
添加回答
举报
0/150
提交
取消