如何使用 Go 单元测试为 App Engine 指定模板路径
当使用 App Engine 和 Go 的内置模板包时,单元测试查找模板文件时可能会遇到问题。这是因为,在本地开发过程中,服务器会查找相对于应用程序根目录的模板文件,而单元测试在不同的目录中运行。
问题
The单元测试出现恐慌,并显示以下消息:“恐慌:打开 templates/index.html:没有这样的文件或目录。”这表明服务器找不到index.html模板文件。
选项1:更改工作目录
一个选项是将工作目录更改为应用程序根目录在调用使用模板相对路径的代码之前。这可以通过 os.Chdir() 来实现。
import "os" func init() { if err := os.Chdir("../.."); err != nil { panic(err) } }
选项 2:重构代码
另一个选项是重构使用相对路径接受的代码基本路径。测试时可以将此基本路径设置为应用程序根目录,从而使相对路径能够正常运行。
func pageIndex(w http.ResponseWriter, r *http.Request, basePath string) { tpls := append([]string{basePath + "/templates/index.html"}, templates...) tpl := template.Must(template.ParseFiles(tpls...)) // ... }
在单元测试中,可以将基本路径设置为应用程序根目录,确保模板可以找到文件。
func TestPageIndex(t *testing.T) { inst, _ := aetest.NewInstance(nil) //ignoring the errors for brevity defer inst.Close() req, _ := inst.NewRequest("GET", "/", nil) resp := httptest.NewRecorder() pageIndex(resp, req, "../..") // Set base path to app root }
以上是如何解决 Go App Engine 单元测试中的'恐慌:打开 templates/index.html:没有这样的文件或目录”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

go'serrorhandlingurturnserrorsasvalues,与Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

AnefactiveInterfaceoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForeabStractionTosWapImplementations withCallingCallingCode.3)

集中式错误处理在Go语言中可以提升代码的可读性和可维护性。其实现方式和优势包括:1.将错误处理逻辑从业务逻辑中分离,简化代码。2.通过集中处理错误,确保错误处理的一致性。3.使用defer和recover来捕获和处理panic,增强程序健壮性。

Ingo,替代词Inivuntionsionializatializatializationfunctionsandsingletons.1)customInitializationfunctions hallowexpliticpliticpliticconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconcontirization curssementializatizatupsetups.2)单次固定元素限制ininconinconcurrent

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go语言的错误处理通过errors.Is和errors.As函数变得更加灵活和可读。1.errors.Is用于检查错误是否与指定错误相同,适用于错误链的处理。2.errors.As不仅能检查错误类型,还能将错误转换为具体类型,方便提取错误信息。使用这些函数可以简化错误处理逻辑,但需注意错误链的正确传递和避免过度依赖以防代码复杂化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版
好用的JavaScript开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版