如何使用Beego创建API测试文件

3

我有以下测试文件:

package tests

import (
    "net/http"
    "net/http/httptest"
    "testing"
    "runtime"
    "path/filepath"
    _ "hello/routers"
    _ "github.com/lib/pq"

    "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", "/api/testing", nil)
    w := httptest.NewRecorder()
    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)
            })
    })
}

当我运行go test tests/*.go时,会得到以下结果:
2015/01/14 23:55:19 [config.go:284] [W] open /home/IdeaProjects/go/src/hello/tests/conf/app.conf: no such file or directory 
[ORM]register db Ping `default`, pq: password authentication failed for user "hello"
must have one register DataBase alias named `default`
FAIL    command-line-arguments  0.008s

我已经使用 bee api 来引导Beego,然后使用pq Postgres驱动程序来连接PG数据库。

此外,我不确定为什么它会查找位于/hello/tests/conf/app.conf路径下的app.conf文件,它应该查找/hello/conf/app.conf

这是为什么发生的原因吗?


嗨,你查明了测试的情况吗?我有一个类似的问题,我的应用程序路由没有被识别出来。编辑:我发现我需要在测试中导入我的路由包才能识别它们...哎呀 - Sekm
我也遇到了同样的问题,它试图在测试中找到配置而不是项目。你找到解决方法了吗,@热情的开发者? - souravlahoti
1个回答

0

PG 无法验证您的身份,可能是因为它在 app.conf 文件中找不到密码,也找不到该文件,因为它正在错误的位置查找。

尝试使用绝对路径来配置您的配置文件,而不是在您的 init() 函数中使用复杂的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接