我有以下测试文件:
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/