有一个我要负责测试的功能的怪物(我没有编辑权限),它击中了html/template
包中的函数ParseFiles
,ExecuteTemplates
< / p>
因此定义了它的业务端:
variables.mutex.RLock()
tc, ok := variables.templateCollection[view.Name]
variables.mutex.RUnlock()
// Get the plugin collection
pc := make(template.FuncMap)
variables.mutexPlugins.RLock()
for k, v := range variables.pluginCollection {
pc[k] = v
}
variables.mutexPlugins.RUnlock()
//-------Template func maps for the static navs using session---
for _, m := range customPlugins {
for k, v := range m {
pc[k] = v
}
}
//----------------------------------------------------
// If the template collection is not cached or caching is disabled
if !ok || !variables.viewInfo.Caching {
// Determine if there is an error in the template syntax
if templates, err := template.New(view.Name).Funcs(pc).ParseFiles(allTemplates...); err != nil {
http.Error(w, "Template Parse Error: "+err.Error(), http.StatusInternalServerError)
return
} else {
// Cache the template collection
variables.mutex.Lock()
variables.templateCollection[view.Name] = templates
variables.mutex.Unlock()
// Save the template collection
tc = templates
}
}
// Display the content to the screen
var err error
if !w.Written() {
if loadFull {
err = tc.Funcs(pc).ExecuteTemplate(w, variables.rootTemplate+"."+view.Extension, view.Vars)
} else {
viewPath := strings.Split(view.Name, "/")
viewName := viewPath[len(viewPath)-1]
err = tc.Funcs(pc).ExecuteTemplate(w, viewName+"."+view.Extension, view.Vars)
}
}
我没有看到可以进行单元测试的任何方式。从理论上讲,我可以使用httptest.NewRecorder()
,甚至可以设置一个模拟ResponseWriter,因为w
的类型为web.ResponseWriter
,它扩展了接口http.ResponseWriter
。有没有一种测试代码的方法,它们本身会命中模板功能,即ParseFiles
功能?