Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 马提尼模板和测试_Go_Martini - Fatal编程技术网

Go 马提尼模板和测试

Go 马提尼模板和测试,go,martini,Go,Martini,我正在尝试将应用程序文件与测试文件分离。它看起来像这样: main.go views/ layouts/ layout.html spec/ main_test.go main.go创建一个马提尼应用程序,并告诉Martini.render在何处查找视图: func CreateApplication() { m := martini.Classic() m.Use(render.Renderer(render.Options{ Directory: "view

我正在尝试将应用程序文件与测试文件分离。它看起来像这样:

main.go
views/
  layouts/
    layout.html
spec/
  main_test.go
main.go
创建一个马提尼应用程序,并告诉
Martini.render
在何处查找视图:

func CreateApplication() {
  m := martini.Classic()
  m.Use(render.Renderer(render.Options{
    Directory: "views",
    Layout: "layouts/layout",
    Extensions: []string{".html"},
  }))
}
当我从根文件夹中使用
go-run
时,这一切都非常有效。但是,当我尝试使用
spec/main\u test.go
文件中的
CreateApplication()
函数时,它现在正在
spec/views
中查找视图,因为那是run文件夹

我尝试使用
runtime.Caller()
来获取绝对路径,但在编译二进制文件时,这完全搞砸了


我想我的问题是,我如何才能让这一切顺利进行?我希望
CreateApplication()
无论从何处调用都能正常工作。

我经常遇到这个问题。在这种情况下,我要做的是创建一个从子目录到根目录中存放模板的文件夹的符号链接。到目前为止,我在使用这个appraoch时没有遇到任何问题,但是当一个应用程序投入生产时,我会删除这些符号链接。实际上,我有一个脚本,可以在开始测试之前创建符号链接,然后在完成测试后删除它们

在你的例子中,它看起来像这样(我在Ubuntu或Cygwin上):

main.go
观点/
布局/
layout.html
规格/
main_test.go
$cd规格
$ln-s../views
梅因,加油
观点/
布局/
layout.html
规格/
main_test.go

视图使用绝对路径而不是相对路径?编辑为“绝对”而不是“相对”。如果我真的能得到一条绝对路径并且它能工作,我很想看看代码。我无法获取
os.Getwd()
filepath.Join
来工作。我最后只是传入了一个字符串,其中包含根目录的相对路径。在此处跟踪进度:。是否有助于获取该绝对路径?(使用其Cwd功能:)
main.go
views/
  layouts/
    layout.html
spec/
  main_test.go

$ cd spec
$ ln -s ../views views

main.go
views/
  layouts/
    layout.html
spec/
  main_test.go
  views <- this is the symlink