postgresql - 如何用Beego制作API测试文件

标签 postgresql go beego

我有以下测试文件:

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 Driver for PG database。

另外,我不确定为什么它会查看 /hello/tests/conf/app.conf 路径以查找 app.conf 文件,它应该查找 /hello/conf/app.conf.

发生这种情况是否有原因?

最佳答案

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

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

关于postgresql - 如何用Beego制作API测试文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943563/

相关文章:

sql - 如何在连接的聚合查询中聚合字段中的多行

sql - PostgreSQL 中的递归 CTE 问题

go - 如何将 JSON 解码为 dynamodb.AttributeValue?

go - Prometheus客户过早清理柜台?

oop - 使用反射调用方法并返回值

mysql - 如何避免 "too many connections"

postgresql - 如何在 Heroku Postgres 数据库中查找 "missing"行?

go - Beego:显示从表单输入字段中获取的登录用户名

go - Beego - 端点测试

sql - 如果连接表中的任何关联行的特定列为 NULL,我该如何排除结果?