Go 作为单元测试用例,我们应该为这样的函数编写什么。代码片段会很有帮助

Go 作为单元测试用例,我们应该为这样的函数编写什么。代码片段会很有帮助,go,Go,在代码中,检查并记录错误,然后继续并忽略错误。请仔细检查-我该如何问一个好问题?您不应该有解析模板的处理程序。请参阅:。一旦将模板解析移出处理程序,测试也就变得容易多了。一个好的单元测试测试所有有意义的输入值,并确保它们生成所有有意义的输出值。输入包括全局状态,例如全局变量、数据库或文件系统。输出包括副作用(例如修改全局变量、更改数据库条目或写入文件系统)。鉴于此:此功能的有意义输入是什么?考虑到这些输入,输出应该是什么?编写涵盖这些情况的测试。 func Home(w http.Respons

在代码中,检查并记录错误,然后继续并忽略错误。

请仔细检查-我该如何问一个好问题?您不应该有解析模板的处理程序。请参阅:。一旦将模板解析移出处理程序,测试也就变得容易多了。一个好的单元测试测试所有有意义的输入值,并确保它们生成所有有意义的输出值。输入包括全局状态,例如全局变量、数据库或文件系统。输出包括副作用(例如修改全局变量、更改数据库条目或写入文件系统)。鉴于此:此功能的有意义输入是什么?考虑到这些输入,输出应该是什么?编写涵盖这些情况的测试。
func Home(w http.ResponseWriter, r *staticAuth.AuthenticatedRequest) {
    t, err := template.ParseFiles("index.html") //parse the html file homepage.html
    if err != nil {                             // if there is an error
        log.Print("template parsing error: ", err) // log it
    }
    err = t.Execute(w, nil) //execute the template and pass it the HomePageVars struct to fill in the gaps
    if err != nil {         // if there is an error
        log.Print("template executing error: ", err) //log it
    }
}
func Home(w http.ResponseWriter, r *staticAuth.AuthenticatedRequest) {
    t, err := template.ParseFiles("index.html") //parse the html file homepage.html
    if err != nil {                             // if there is an error
        log.Print("template parsing error: ", err) // log it
    }
    err = t.Execute(w, nil) //execute the template and pass it the HomePageVars struct to fill in the gaps
    if err != nil {         // if there is an error
        log.Print("template executing error: ", err) //log it
    }
}