使用模板 golang 进行单元测试
Unittest with template golang
我有一个非常简单的为模板提供服务的 http 处理程序 about.html
func AboutPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
template, err := template.ParseFiles("templates/about.html")
if err != nil {
logfile.ErrorMsg(fmt.Sprint(err.Error()))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template.Execute(w, nil)
}
它工作正常,但是当我想运行这个单元测试时:
func getRequest(t testing.TB, url string) *http.Request {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatal(err)
}
return req
}
func TestAboutPage(t *testing.T) {
r := getRequest(t, "/about")
rw := httptest.NewRecorder()
AboutPage(rw, r, httprouter.Params{})
}
程序因此崩溃:
go test
--- FAIL: TestAboutPage (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x41123b4]
goroutine 19 [running]:
panic(0x4417520, 0xc42000c0b0)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
testing.tRunner.func1(0xc42009e180)
/usr/local/go/src/testing/testing.go:579 +0x25d
panic(0x4417520, 0xc42000c0b0)
/usr/local/go/src/runtime/panic.go:458 +0x243
html/template.(*Template).escape(0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:79 +0x44
html/template.(*Template).Execute(0x0, 0x52683b0, 0xc42007a7c0, 0x0, 0x0, 0xc420077290, 0x1)
/usr/local/go/src/html/template/template.go:101 +0x2f
github.com/engineerbeard/engineerbeard.com/httpHandlers.AboutPage(0x45ee2e0, 0xc42007a7c0, 0xc4200ee0f0, 0xc42004bf38, 0x0, 0x0)
/Users/dbubel/gowork/src/github.com/engineerbeard/engineerbeard.com/httpHandlers/handler.go:122 +0xbd
github.com/engineerbeard/engineerbeard.com/httpHandlers.TestAboutPage(0xc42009e180)
/Users/dbubel/gowork/src/github.com/engineerbeard/engineerbeard.com/httpHandlers/handlers_test.go:26 +0x15c
testing.tRunner(0xc42009e180, 0x449b320)
/usr/local/go/src/testing/testing.go:610 +0x81
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:646 +0x2ec
exit status 2
FAIL github.com/engineerbeard/engineerbeard.com/httpHandlers 0.012s
如果我用简单的 Fprintf(w, "foo") 替换 template.execute() 测试 运行 没问题。那么如何使用模板进行单元测试呢?
好的,我想我明白了,我正在使用
_ "github.com/mattn/go-sqlite3"
作为进口。我 运行 去那个目录安装,现在它运行了...
如果在这个行模板中加上绝对路径就可以解决,err := template.ParseFiles("absolutePathOfTheFile")
我有一个非常简单的为模板提供服务的 http 处理程序 about.html
func AboutPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
template, err := template.ParseFiles("templates/about.html")
if err != nil {
logfile.ErrorMsg(fmt.Sprint(err.Error()))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template.Execute(w, nil)
}
它工作正常,但是当我想运行这个单元测试时:
func getRequest(t testing.TB, url string) *http.Request {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatal(err)
}
return req
}
func TestAboutPage(t *testing.T) {
r := getRequest(t, "/about")
rw := httptest.NewRecorder()
AboutPage(rw, r, httprouter.Params{})
}
程序因此崩溃:
go test
--- FAIL: TestAboutPage (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x41123b4]
goroutine 19 [running]:
panic(0x4417520, 0xc42000c0b0)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
testing.tRunner.func1(0xc42009e180)
/usr/local/go/src/testing/testing.go:579 +0x25d
panic(0x4417520, 0xc42000c0b0)
/usr/local/go/src/runtime/panic.go:458 +0x243
html/template.(*Template).escape(0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:79 +0x44
html/template.(*Template).Execute(0x0, 0x52683b0, 0xc42007a7c0, 0x0, 0x0, 0xc420077290, 0x1)
/usr/local/go/src/html/template/template.go:101 +0x2f
github.com/engineerbeard/engineerbeard.com/httpHandlers.AboutPage(0x45ee2e0, 0xc42007a7c0, 0xc4200ee0f0, 0xc42004bf38, 0x0, 0x0)
/Users/dbubel/gowork/src/github.com/engineerbeard/engineerbeard.com/httpHandlers/handler.go:122 +0xbd
github.com/engineerbeard/engineerbeard.com/httpHandlers.TestAboutPage(0xc42009e180)
/Users/dbubel/gowork/src/github.com/engineerbeard/engineerbeard.com/httpHandlers/handlers_test.go:26 +0x15c
testing.tRunner(0xc42009e180, 0x449b320)
/usr/local/go/src/testing/testing.go:610 +0x81
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:646 +0x2ec
exit status 2
FAIL github.com/engineerbeard/engineerbeard.com/httpHandlers 0.012s
如果我用简单的 Fprintf(w, "foo") 替换 template.execute() 测试 运行 没问题。那么如何使用模板进行单元测试呢?
好的,我想我明白了,我正在使用
_ "github.com/mattn/go-sqlite3"
作为进口。我 运行 去那个目录安装,现在它运行了...
如果在这个行模板中加上绝对路径就可以解决,err := template.ParseFiles("absolutePathOfTheFile")