1 Star 0 Fork 1

maxiao / goview

forked from mirrors_foolin / goview 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
instance_test.go 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
laufin 提交于 2019-05-08 22:48 . add test render
package goview
import (
"html/template"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestDefault(t *testing.T) {
recorder := httptest.NewRecorder()
err := Render(recorder, http.StatusOK, "index", M{})
//expect like this info:
// "error: ViewEngine render read name:layouts/master, path:/foolin/goview/views/layouts/master.html, error: open /foolin/goview/views/layouts/master.html: The system cannot find the path specified"
if err == nil {
t.Error("render is ok?")
} else {
t.Logf("expect error: %v", err)
}
}
func TestRender(t *testing.T) {
engine := New(Config{
Root: "_examples/test",
Extension: ".tpl",
Master: "layouts/master",
Partials: []string{},
Funcs: template.FuncMap{
"echo": func(v string) string {
return "$" + v
},
},
DisableCache: true,
})
Use(engine)
recorder := httptest.NewRecorder()
expect := "<v>Index</v>"
err := Render(recorder, http.StatusOK, "index", M{})
if err != nil {
t.Errorf("render error: %v", err)
return
}
assertRecorder(t, recorder, http.StatusOK, expect)
}
func assertRecorder(t *testing.T, recorder *httptest.ResponseRecorder, expectStatusCode int, expectOut string) {
result := recorder.Result()
if result.StatusCode != expectStatusCode {
t.Errorf("actual: %v, expect: %v", result.StatusCode, expectStatusCode)
}
resultBytes, err := ioutil.ReadAll(result.Body)
if err != nil {
t.Errorf("read result body error: %v", err)
return
}
val := string(resultBytes)
if val != expectOut {
t.Errorf("actual: %v, expect: %v", val, expectOut)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mx_space/goview.git
git@gitee.com:mx_space/goview.git
mx_space
goview
goview
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891