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

下面是我正在检查我的API端点之一的测试用例。</ p>

< pre> 程序包测试

导入(
“ net / http”
“ net / http / httptest”
“路径/文件路径”
“运行时”
“测试”

“ 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是运行端点测试的示例
func TestGet(t * testing.T){
r,_:= http.NewRequest(“ GET”,“ / v1 / Bangalore / feed / 24”,nil)\ nw:= 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”,“代码[% d]
%s“,w.Code,w.Body.String())

y(“ Subject:Test Station Endpoint
”,t,func(){
Convey(“状态代码应为200”,func(){
So(w.Code,ShouldEqual,200)
})

Convey(“结果不应为空”,func(){
So(w.Body.Len(),ShouldBeGreaterThan,0)
})
})
}
</ code> </ pre>

在本地运行Beego服务器并通过Postman请求时,我收到响应,状态为200 OK,但是在运行测试用例时,它返回404状态代码。</ p>

我的app.conf文件位于conf文件夹中。</ p>

  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” \ nmsSqlPort = 1434
msSqlDBName =“ xxxxx”
</ code> </ pre>
</ div>

展开原文

原文

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个回答



您需要初始化路由器。您可以添加</ p>

  import _“ path  / routers“ 
</ code> </ pre>

(如果路由在init()函数中。)</ p>
</ div>

展开原文

原文

You need to initialize the routers.You can add

import _ "path/routers"

if the routes are in init() function.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐