【问题标题】:Go Martini Unit Test ExampleGo Martini 单元测试示例
【发布时间】:2015-06-20 19:24:58
【问题描述】:

我对 Go 很陌生,想知道是否有任何约定/标准以及如何测试 Go Martini 的处理程序代码的示例?

提前感谢您!

【问题讨论】:

    标签: unit-testing go martini


    【解决方案1】:

    martini-contrib 库有很多现有代码值得一看:https://github.com/martini-contrib/secure/blob/master/secure_test.go

    例如

    func Test_No_Config(t *testing.T) {
        m := martini.Classic()
        m.Use(Secure(Options{
        // nothing here to configure
        }))
    
        m.Get("/foo", func() string {
            return "bar"
        })
    
        res := httptest.NewRecorder()
        req, _ := http.NewRequest("GET", "/foo", nil)
    
        m.ServeHTTP(res, req)
    
        expect(t, res.Code, http.StatusOK)
        expect(t, res.Body.String(), `bar`)
    }
    

    总结:

    1. 使用martini.Classic() 创建服务器
    2. 创建到要测试的处理程序的路由
    3. 对响应记录器执行它
    4. 检查响应记录器上的结果(状态代码、正文)是否符合预期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多