doujingao6210 2016-10-19 11:42
浏览 1203
已采纳

Beego测试用例返回404状态,端点不匹配

Below is my test case where I am checking one of my API endpoints.

package test

import (
    "net/http"
    "net/http/httptest"
    "path/filepath"
    "runtime"
    "testing"

    "github.com/astaxie/beego"
    . "github.com/smartystreets/goconvey/convey"
)

func init() {
    _, file, _, _ := runtime.Caller(1)
    apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator))))
    beego.TestBeegoInit(apppath)
}

// TestGet is a sample to run an endpoint test
func TestGet(t *testing.T) {
    r, _ := http.NewRequest("GET", "/v1/Bangalore/feed/24", nil)
    w := httptest.NewRecorder()
    w.Header().Set("Content-Type", "application/json")
    w.Header().Set("Token", "O0h3NFbxxxxxxxxxauBvFZA")
    w.Header().Set("Channel", "xxxx")
    beego.BeeApp.Handlers.ServeHTTP(w, r)

    beego.Trace("testing", "TestGet", "Code[%d]
%s", w.Code, w.Body.String())

    Convey("Subject: Test Station Endpoint
", t, func() {
        Convey("Status Code Should Be 200", func() {
            So(w.Code, ShouldEqual, 200)
        })
        Convey("The Result Should Not Be Empty", func() {
            So(w.Body.Len(), ShouldBeGreaterThan, 0)
        })
    })
}

When running the Beego server locally and requesting through Postman I receive the response and with status 200 OK but when I run the test case , it returns 404 status code.

My app.conf file is in conf folder.

appname = jobfeed
httpport = 8080
runmode = dev
autorender = false
copyrequestbody = true
EnableDocs = true
EnableAdmin = false

[dev]
mongourls = "xxxxxx"
mongodb   = "xxxxx"
URL = "xxxx"
msSqlURL = "xxxxx"
msSqlUsername = "xx"
msSqlPassword = "xxx"
msSqlPort = 1434
msSqlDBName = "xxxxx"
  • 写回答

1条回答 默认 最新

  • donglan8870 2016-10-20 18:33
    关注

    You need to initialize the routers.You can add

    import _ "path/routers"
    

    if the routes are in init() function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog