//This is my controller test function that test for GET Item request
func TestGetItem(t *testing.T) {
s := GetDockerItemControllerImpl{
GetDockerService: GetItemReqSuccessImplTest{},
}
for _, test := range testCaseGetItemFullSuccess {
router := mux.NewRouter()
router.HandleFunc("/docker/config/{id}", s.GetDockerConfig())//this will run controller function GetDockerConfig()
ts := httptest.NewServer(router)
defer ts.Close()
req, err := http.NewRequest(test.Method, test.URL, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if status := rr.Code; status != test.expectStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, test.expectStatus)
}
}
}
//Controller function
func (s *GetDockerItemControllerImpl) GetDockerConfig() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
var rootobject models.Root
vars := mux.Vars(req)
if bson.IsObjectIdHex(vars["id"]) {
rootobject.ID = bson.ObjectIdHex(vars["id"])
marshalData, unmarshalErr := json.Marshal(rootobject)
if unmarshalErr != nil {
http.Error(w, "Unprocessable Entity error", http.StatusUnprocessableEntity)
return
}
rootobject, err := s.GetDockerService.GetItem(marshalData)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
t, err := template.ParseFiles("./views/dockerconfigDetails.gtpl")
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
t.ExecuteTemplate(w, "dockerconfigDetails.gtpl", rootobject)
if err != nil {
http.Error(w, "Record not found of this ID:"+vars["id"], http.StatusNotFound)
return
}
} else {
http.Error(w, "Invalid Id bad request", http.StatusBadRequest)
}
}}
- 这是我执行模板的 Controller 函数。 - 在 Controller 测试功能中,我创建了一个模拟服务器,它可以访问特定的 URL。 - 在运行 get test -v 命令时我得到这个----失败:TestGetItem(0.00s) panic :运行时错误:无效内存地址或 nil 指针取消引用 [已恢复] panic :运行时错误:无效的内存地址或零指针取消引用 [信号 SIGSEGV:分段违规代码=0x1 地址=0x20 pc=0x5927a0]
goroutine 9 [running]:
testing.tRunner.func1(0xc4201263c0)
/home/webonise/niketa/go/src/testing/testing.go:711 +0x2d2
panic(0x7cc060, 0xa5bcc0)
/home/webonise/niketa/go/src/runtime/panic.go:491 +0x283
html/template.(*Template).lookupAndEscapeTemplate(0x0, 0x845b99, 0x18, 0x0, 0x0, 0x0)
/home/webonise/niketa/go/src/html/template/template.go:144 +0x50
html/template.(*Template).ExecuteTemplate(0x0, 0x7f9afab510e8, 0xc420060b80, 0x845b99, 0x18, 0x808400, 0xc42011c880, 0x0, 0x0)
/home/webonise/niketa/go/src/html/template/template.go:133 +0x43
github.com/gror/controllers.(*GetDockerItemControllerImpl).GetDockerConfig.func1(0xa2f540, 0xc420060b80, 0xc42012a500)
/home/webonise/go/src/github.com/gror/controllers/controller.go:149 +0x3ce
net/http.HandlerFunc.ServeHTTP(0xc4200472a0, 0xa2f540, 0xc420060b80, 0xc42012a500)
/home/webonise/niketa/go/src/net/http/server.go:1918 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc420128a80, 0xa2f540, 0xc420060b80, 0xc42012a500)
/home/webonise/go/src/github.com/gorilla/mux/mux.go:162 +0xed
github.com/gror/controllers.TestGetItem(0xc4201263c0)
/home/webonise/go/src/github.com/gror/controllers/controller_test.go:364 +0x500
testing.tRunner(0xc4201263c0, 0x8575a8)
/home/webonise/niketa/go/src/testing/testing.go:746 +0xd0
created by testing.(*T).Run
/home/webonise/niketa/go/src/testing/testing.go:789 +0x2de
exit status 2
FAIL github.com/gror/controllers 0.007s
最佳答案
如果你想测试一个包含在特定方法中执行的模板的方法,那么你必须提供绝对路径。
请按照以下步骤操作:
在终端中设置当前工作目录:
export varname="absolutepath"
例如:
export PATH="/go/src/github.com/project1"
在您的终端中使用此命令:
echo $varname
例如:
echo $PATH
/go/src/github.com/project1
它将为您提供您设置的完整路径。
在你的程序中使用这个:
path := os.Getenv("PATH")
t, err := template.ParseFiles(Path + "views/dockerconfig.gtpl")
if err != nil {
fmt.Println(errors.New("unable to execute the template"))
}
t.Execute(w, nil)
关于go - 无法在golang中测试模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51041516/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳