beego / bee

Bee is a tool for helping develop with beego app framework.
Apache License 2.0
1.45k stars 921 forks source link

default_test.go generated by bee fails to utilize beego/v2/core/logs #763

Closed gnosthi closed 3 years ago

gnosthi commented 3 years ago

While this is only a minor bug and should be easy enough to fix, its worth pointing out that the tests/default_test.go file that is generated by bee does not utilize the 2.0 logging mechanism but the old logging mechanism. It will therefore fail when doing go test ./tests/.

The offending code is:

import (
[...]
beego "github.com/beego/beego/v2/server/web"
[...]
)

[...]
func TestBeego(t *testing.T) {
 [...]
beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
[...]
}

This should include the new logging mechanism like so.

package test

import (
    "net/http"
    "net/http/httptest"
    "testing"
    "runtime"
    "path/filepath"
    _ "openaltar-webapp/routers"

    beego "github.com/beego/beego/v2/server/web"
    . "github.com/smartystreets/goconvey/convey"
    log "github.com/beego/beego/v2/core/logs"
)

func init() {
    _, file, _, _ := runtime.Caller(0)
    apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
    beego.TestBeegoInit(apppath)
}

// TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) {
    r, _ := http.NewRequest("GET", "/", nil)
    w := httptest.NewRecorder()
    beego.BeeApp.Handlers.ServeHTTP(w, r)

    log.Trace("testing", "TestBeego", "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)
            })
    })
}

While I get that this file is not likely to be used as is, it might still be worth resolving for the sake of feature demonstration completeness.

flycash commented 3 years ago

PR #756 Please check it.