登录
首页 >  Golang >  Go问答

测试 Go Gin 处理函数的方法是什么?

来源:stackoverflow

时间:2024-03-26 14:45:32 404浏览 收藏

为了测试涉及 HTTP 请求的 Go Gin 处理程序函数,需要实际初始化 `*http.Request` 并将其设置为 Gin 上下文。对于 `c.bindquery`,可以模拟请求的 URL 和 `url.RawQuery`。如果需要模拟 JSON 绑定,可以将测试查询参数添加到请求 URL 中。对于服务调用,理想情况下它应该是一个接口,可以通过注入依赖项或将其设置为 Gin 上下文值来实现可测试性。

问题内容

我有一个像这样的控制器功能......

func getmaterialbyfilter(c *gin.context) {

    queryparam := weldprogs.queryparam{}
    c.bindquery(&queryparam)
    materialbyfilter, geterr := services.weldprogservice.getmaterialbyfilter(&queryparam)
    if geterr != nil {
        //todo : handle user creation error
        c.json(geterr.status, geterr)
        return
    }
    c.json(http.statusok, materialbyfilter)

}

queryparam结构是这样的..

type queryparam struct {
    basematgroup_id []string `form:"basematgroup_id"`
    license_id      []string `form:"license_id"`
    diameter_id     []string `form:"diameter_id"`
    gasgroup_id     []string `form:"gasgroup_id"`
    wiregroup_id    []string `form:"wiregroup_id"`
    wiremat_id      []string `form:"wiremat_id"`
}

我的测试函数是这样的..

func testgetmaterialbyfilter(t *testing.t) {
    w := httptest.newrecorder()
    c, _ := gin.createtestcontext(w)
    getmaterialbyfilter(c)
    assert.equal(t, 200, w.code) 

    var got gin.h
    err := json.unmarshal(w.body.bytes(), &got)
    if err != nil {
        t.fatal(err)
    }
    fmt.println(got)
    assert.equal(t, got, got) 
}

运行此测试时,出现以下错误

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x97f626]

但是当我注释掉控制器函数中的 c.bindquery() 行时,它成功运行了我的测试函数。我在这里做错了什么?我可以以某种方式模拟 c.bindquery 函数吗?


正确答案


要测试涉及 http 请求的操作,您必须实际初始化 *http.request 并将其设置为 gin 上下文。要专门测试 c.bindquery ,正确初始化请求的 urlurl.rawquery 就足够了:

func mockgin() (*gin.context, *httptest.responserecorder) {
    w := httptest.newrecorder()
    c, _ := gin.createtestcontext(w)

    // test request, must instantiate a request first
    req := &http.request{
        url:    &url.url{},
        header: make(http.header), // if you need to test headers
    }
    // example: req.header.add("accept", "application/json")

    // request query
    testquery := weldprogs.queryparam{/* init fields */}

    q := req.url.query()
    for _, s := range testquery.basematgroup_id {
        q.add("basematgroup_id", s)
    }
    // ... repeat for other fields as needed

    // must set this, since under the hood c.bindquery calls
    // `req.url.query()`, which calls `parsequery(u.rawquery)`
    req.url.rawquery = q.encode()
    
    // finally set the request to the gin context
    c.request = req

    return c, w
}

如果您需要模拟 json 绑定,请参阅 this answer

无法按原样测试服务调用 services.weldprogservice.getmaterialbyfilter(&queryparam)。为了可测试,它必须(理想情况下)是一个接口,并以某种方式注入为处理程序的依赖项。

假设它已经是一个接口,为了使其可注入,您要么需要它作为处理程序参数 - 但这会迫使您更改处理程序的签名 - 或者您将其设置为 gin 上下文值: p>

func getmaterialbyfilter(c *gin.context) {
    //...
    weldprogservice := mustgetservice(c)
    materialbyfilter, geterr := weldprogservice.getmaterialbyfilter(&queryparam)
    // ...
}

func mustgetservice(c *gin.context) services.weldprogservice {
    svc, exists := c.get("svc_context_key")
    if !exists {
        panic("service was not set")
    }
    return svc.(services.weldprogservice)
}

然后你可以在单元测试中模拟它:

type mockSvc struct {
}

// have 'mockSvc' implement the interface 

func TestGetMaterialByFilter(t *testing.T) {
    w := httptest.NewRecorder()
    c, _ := gin.CreateTestContext(w)

    // now you can set mockSvc into the test context
    c.Set("svc_context_key", &mockSvc{})

    GetMaterialByFilter(c)
    // ... 
}

今天关于《测试 Go Gin 处理函数的方法是什么?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>