Beego测试用例返回404状态,端点不匹配 [英] Beego Test Case returns 404 status, endpoint not match

查看:826
本文介绍了Beego测试用例返回404状态,端点不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]\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,但是当我运行测试用例时,它返回404状态代码。

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.

我的app.conf文件位于conf文件夹中。

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"


推荐答案

您需要初始化路由器。您可以添加

You need to initialize the routers.You can add

import _ "path/routers"

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

if the routes are in init() function.

这篇关于Beego测试用例返回404状态,端点不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆