在 Gin Go 框架中测试

问题描述

我到处搜索有关如何解决此问题的信息。我没有完全覆盖在 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...