unit-testing - beego 测试用例返回 404 状态,端点不匹配

标签 unit-testing go http-status-code-404 beego

下面是我的测试用例,我正在检查我的 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 但是当我运行测试用例时,它返回 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"

最佳答案

你需要初始化路由器,你可以添加

import _ "path/routers"

如果路由在 init() 函数中。

关于unit-testing - beego 测试用例返回 404 状态,端点不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40130473/

相关文章:

c# - Assert.AreEqual 如何确定两个通用 IEnumerable 之间的相等性?

go - 为什么 Golang GORM Hook BeforeUpdate,AfterUpdate 不触发?

.htaccess - ErrorDocument 与 RewriteRule

python - 在 Python 中检测软 404 重定向

ruby - JRuby 垃圾收集在 Rails 中运行单元和功能测试时抛出错误

ruby-on-rails - 运行单轨单元/功能测试

unit-testing - 在域类中测试服务

android - 如何将一个简单的 Go 脚本作为 Android 服务运行?

linux - Golang交叉编译: gccgo - unrecognized command line option `-marm`

web-applications - 如何处理 HTTP 状态 404 并将用户重定向到应用程序重新部署时的不同页面