【问题标题】:Unit Testing With Gin-Gonic使用 Gin-Gonic 进行单元测试
【发布时间】:2020-03-29 21:04:34
【问题描述】:

我的项目分为三个主要组件:控制器、服务和模型。当通过 URI 查询路由时,会调用控制器,然后调用服务与模型交互,模型再通过 gorm 与数据库交互。

我正在尝试为控制器编写单元测试,但我很难理解如何在模拟 gin 层的同时正确模拟服务层。我可以获得一个模拟的杜松子酒上下文,但我无法在我的控制器方法中模拟服务层。以下是我的代码:

resourceController.go

package controllers

import (
    "MyApi/models"
    "MyApi/services"
    "github.com/gin-gonic/gin"
    "net/http"
)

func GetResourceById(c *gin.Context) {
    id := c.Param("id")
    resource, err := services.GetResourceById(id)

    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": err})
        return
    } else if resource.ID == 0 {
        c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "Resource with id:"+id+" does not exist"})
        return
    }

    c.JSON(http.StatusOK, gin.H{
        "id": resource.ID,
        "data1": resource.Data1,
        "data2": resource.Data2,
    })
}

我想测试 c.JSON 是否返回正确的 http 状态和其他数据。我需要模拟id 变量、err 变量和c.JSON 函数,但是当我尝试将测试中的c.JSON 函数设置为我的新函数时,我收到一条错误消息Cannot assign to c.JSON。 以下是我编写测试的尝试:

resourceController_test.go

package controllers

import (
    "github.com/gin-gonic/gin"
    "github.com/stretchr/testify/assert"
    "net/http/httptest"
    "testing"
)

func TestGetResourceById(t *testing.T) {
    var status int
    var body interface{}
    c, _ := gin.CreateTestContext(httptest.NewRecorder())
    c.JSON = func(stat int, object interface{}) {
        status = stat
        body = object
    }
    GetResourceById(c)
    assert.Equal(t, 4, 4)
}

如何正确编写单元测试来测试 c.JSON 是否返回正确的值?

【问题讨论】:

    标签: unit-testing go testing go-gin


    【解决方案1】:

    你不能在 Go 中修改一个类型的方法。它由在编译时定义类型的包定义且不可变。这是 Go 的设计决定。干脆不要这样做。

    您已经使用httptest.NewRecorder() 作为gin.Context.ResponseWriter 的模拟,它将记录写入响应的内容,包括c.JSON 调用。但是,您需要保留httptest.ReponseRecorder 的引用,然后再检查。请注意,您只有一个编组的 JSON,因此您需要解组它以检查内容(因为 Go 映射和 JSON 对象的顺序都无关紧要,检查编组字符串的相等性很容易出错)。

    例如,

    func TestGetResourceById(t *testing.T) {
        w := httptest.NewRecorder()
        c, _ := gin.CreateTestContext(w)
        GetResourceById(c)
        assert.Equal(t, 200, w.Code) // or what value you need it to be
    
        var got gin.H
        err := json.Unmarshal(w.Body.Bytes(), &got)
        if err != nil {
            t.Fatal(err)
        }
        assert.Equal(t, want, got) // want is a gin.H that contains the wanted map.
    }
    

    【讨论】:

    • 这不是实际发出请求而不是模拟请求吗?
    • 另外,gin.Engine 没有 StatusBody 方法。
    • @EvanBloemer 抱歉,代码输入错误,非常混乱。现在修好了。 w 应该是 httptest.NewRecorder() 的值。
    • @EvanBloemer 它没有经过框架(路由、解析请求等),所以我会说它是在模拟请求。它背后没有网络连接,所以我会说它适合作为单元测试。
    • 在上面的例子中如何将json作为请求参数传递?
    【解决方案2】:

    基于testing 部分,您可以执行以下操作:

    func TestGetResourceById(t *testing.T) {
        router := setupRouter()
    
        w := httptest.NewRecorder()
        req, _ := http.NewRequest("GET", "/GetResourceById", nil)
        router.ServeHTTP(w, req)
    
        assert.Equal(t, 200, w.Code)
        assert.Equal(t, "your expected output", w.Body.String())
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-02
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2020-03-03
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多