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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序