Beego Test Case returns 404 status, endpoint not match

泪湿孤枕 提交于 2019-12-11 05:27:09

问题


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)
        })
    })
}

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:


You need to initialize the routers.You can add

import _ "path/routers"

if the routes are in init() function.



来源:https://stackoverflow.com/questions/40130473/beego-test-case-returns-404-status-endpoint-not-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!