问题描述
我到处搜索有关如何解决此问题的信息。我没有完全覆盖在 Go 中测试 Gin GET 调用。
我正在调用测试,该函数的结果为 50%,但我似乎无法将结果提高到 100%。
Here is the code and the coverage output html
这是 Go 代码
var JSONGET = func(c *gin.Context) {
c.IndentedJSON(http.StatusOK,gin.H{
"code": http.StatusOK,"endpoint": c.Request.RequestURI,"response": gin.H{
"body": "GET Response","type": "text",},"status": "http.StatusOK","type": "GET",})
}
// PingGET ...
func PingGET(c *gin.Context) gin.HandlerFunc {
return JSONGET
}
这是测试函数
func TestPingGET(t *testing.T) {
response := httptest.NewRecorder()
context,_ := gin.CreateTestContext(response)
fmt.Println(response.Code)
PingGET(context)
fmt.Println(response.Code)
if response.Code != http.StatusOK {
t.Errorf("Error PingGET Failed")
}
}
这是输出的 html,但您需要查看图片以进一步了解哪些部分没有被测试。
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
)
// JSONGET ...
var JSONGET = func(c *gin.Context) **{
c.IndentedJSON(http.StatusOK,gin.H{
"code": http.StatusOK,"response": gin.H{
"body": "GET Response",})
}**
// PingGET ...
func PingGET(c *gin.Context) gin.HandlerFunc {
return JSONGET
}
是否有人可以解释如何解决这个问题和/或解释这个问题?
解决方法
可以在此处找到答案:GinDocs