Eva*_*mer 6 testing unit-testing go go-gin
我的项目分为三个主要组件:控制器、服务和模型。当通过 URI 查询路由时,将调用控制器,然后调用服务与模型交互,然后通过 gorm 与数据库交互。
我正在尝试为控制器编写单元测试,但我很难理解如何在模拟 gin 层的同时正确模拟服务层。我可以获得一个模拟的 gin 上下文,但我无法在我的控制器方法中模拟服务层。下面是我的代码:
资源控制器
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,
})
}
Run Code Online (Sandbox Code Playgroud)
我想测试 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)
}
Run Code Online (Sandbox Code Playgroud)
如何正确编写单元测试来测试 c.JSON 是否返回正确的值?
lea*_*bop 10
你不能在 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.
}
Run Code Online (Sandbox Code Playgroud)