Есть монстр функционала, за который я отвечаю за тестирование (на котором у меня нет прав редактирования), который поражает функции ParseFiles
, ExecuteTemplates
в пакете html/template
Бизнес-цель определяется следующим образом:
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
func?