Beego 测试用例 returns 404 状态,端点不匹配
Beego Test Case returns 404 status, endpoint not match
下面是我的测试用例,我正在检查我的 API 端点之一。
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]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", 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)
})
})
}
当 运行 在本地连接 Beego 服务器并通过 Postman 请求时,我收到响应并且状态为 200 OK 但是当我 运行 测试用例时,它 returns 404 状态代码.
我的 app.conf 文件在 conf 文件夹中。
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"
需要初始化routers.You可以添加
import _ "path/routers"
如果路由在 init() 函数中。
下面是我的测试用例,我正在检查我的 API 端点之一。
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]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", 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)
})
})
}
当 运行 在本地连接 Beego 服务器并通过 Postman 请求时,我收到响应并且状态为 200 OK 但是当我 运行 测试用例时,它 returns 404 状态代码.
我的 app.conf 文件在 conf 文件夹中。
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"
需要初始化routers.You可以添加
import _ "path/routers"
如果路由在 init() 函数中。