我正在為基于 Gin-Gonic 的服務(wù)編寫(xiě)測(cè)試。我有一個(gè)不接受 gin 上下文作為參數(shù)的處理程序。例子:func (h *handler) Handler1(isTrue bool) func(*gin.Context) { return func(c *gin.Context) { .. Do Something }}我想為這個(gè)方法編寫(xiě)測(cè)試,但我不知道我應(yīng)該如何將模擬 Gin Context 傳遞給它。如果我為接受 Gin 上下文作為參數(shù)的任何其他方法(如下面的 Ping 方法)執(zhí)行此操作,我將這樣做。func (*handler) Ping(c *gin.Context) { c.String(http.StatusOK, "Pong") c.Writer.WriteHeaderNow()} // Method在測(cè)試中:handler := NewHandler()recorder := httptest.NewRecorder()mockGinContext, _ := gin.CreateTestContext(recorder)handler.Ping(mockGinContext)對(duì)于Handler1,我應(yīng)該如何將它傳遞給 TestContext?
如何將 TestContext 傳遞給不接受 Gin 上下文作為參數(shù)的方法?
HUH函數(shù)
2022-06-06 15:31:42