为了账号安全,请及时绑定邮箱和手机立即绑定

如何测试使用中间件的处理程序

如何测试使用中间件的处理程序

Go
芜湖不芜 2022-11-08 16:44:45
如何测试使用中间件的处理程序我正在尝试对使用中间件但不作为依赖项的处理程序进行单元测试。我的处理程序代码如下所示: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 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

在上下文中设置键:c.Set("id", uuid)



查看完整回答
反对 回复 2022-11-08
  • 1 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号