亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何測試使用中間件的處理程序

如何測試使用中間件的處理程序

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 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號