草庐IT

power_unit

全部标签

unit-testing - Gomega 的 Equal() 可以处理多个值吗?

我正在测试使用gomega向服务器发送模拟请求的功能,我想验证1.请求已开始2.请求已完成。为此,我返回两个bool值。在下面的示例中,它们都应评估为真,但值可能会发生变化。我试过这个:g:=gomega.NewGomegaWithT(t)...g.Eventually(func()(bool,bool){...start=falseend=trueif(request.status=="started"){start=true}if(request.status=="complete"){end=true}returnstart,end}).Should(Equal((true,tr

unit-testing - 需要帮助查明中间件测试(Golang)中零引用错误的原因

我在为我的http处理程序(特别是身份验证中间件)编写单元测试时遇到了“无效内存地址或nil指针取消引用”错误。这里是要测试的函数:funcAuthenticate(nexthttp.Handler)http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,req*http.Request){varerrerrorck,err:=req.Cookie("session")iferr!=nil{w.Header().Add("Authorization","expired/invalid")next.ServeHTTP(w

unit-testing - 单元测试 Google 的 Go API 客户端

我目前正在使用Google的APIgoclient使用Go编写工作流.我是Go的新手,在对客户的服务进行单元测试时遇到了麻烦。以下是在GoogleCloud项目中启用API的示例方法。func(gcloudService*GCloudService)EnableApi(projectIdstring,apiIdstring)error{service,err:=servicemanagement.New(gcloudService.Client)iferr!=nil{returnerr}requestBody:=&servicemanagement.EnableServiceReque

unit-testing - 使用 httptest 或 http 包模拟服务器故障

如何在隔离单元测试中使用httptest或http包模拟服务器故障?详细信息:我一直在使用gorillawebsockets,所以mt,msg,err:=t.conn.ReadMessage()mt值必须是-1当服务器宕机时。我尝试了以下作为主要选项:varsrv*httptest.Serversrv=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){_,err:=wsUpgrader.Upgrade(w,r,nil)iferr!=nil{t.Fatal(err)}srv.Clos

unit-testing - 具有 Google Cloud API 依赖项的单元测试代码

我正在编写一个小型slack机器人,它可以帮助我的开发人员从GoogleCloudSQL获取数据库转储。我用https://godoc.org/cloud.google.com/go写的大量打包,但现在我在编写单元测试时遇到了麻烦。我想开源代码供其他人使用,但想贡献一些遵循基本准则的东西,即具有测试覆盖率。我读过很多关于模拟、使用接口(interface)的文章,谷歌库​​因在测试中使用起来很痛苦而臭名昭著,而且谷歌人看不起模拟。但是还是无法迈出编写测试的第一步,例如一段代码:typeStorageClientstruct{client*storage.ClienttargetBuck

unit-testing - 在 Golang 中 stub 函数

有一个函数在我负责单元测试的代码中不断出现(还没测试),定义如下://GetRetreivesanerrorwiththegivenaccesscode.funcGet(codeAccessCode)*ErrorSt{iferr,ok:=Errors[code];ok{err.Stack=utilityCore.GetCleanStack(logger.Instance.GetProjectNames())return&err}err:=NewError(string(ACPanic),fmt.Sprintf("Error'%s'can'tberetreived.",code),ESS

unit-testing - 如何测试我的接口(interface)功能?

Go的第三天,如果这是一个新问题,请原谅;)。我正在创建一个简单的计算器,最终将有许多不同的任务:加法、减法、乘法等等……每个任务都有2个函数:第一个和第二个。packagemainimport("github.com/mytestproj/calculator")typeCalcinterface{First(xint)intSecond(xint)int}funcmain(){x:=16variCalca:=calculator.Add{}i=ai.First(x)i.Second(x)}我目前将所有内容组织为:github.com/mytestproj/calculator/ad

unit-testing - 为什么 `go test -run NotExist` 通过?

如果我运行以下命令gotest-runNotExist响应为通过。鉴于我的测试文件不包含名为TestNotExist的测试,我希望上面的命令返回FAIL 最佳答案 没有-run选项gotest运行所有测试。您使用-run选项不运行所有测试;过滤掉,排除测试(你以要求非排他性测试的名称​​匹配正则表达式模式-但这与讨论点无关):Commandgo,Testpackages:Bydefault,gotestneedsnoarguments.Itcompilesandteststhepackagewithsourceinthecurren

unit-testing - 如何使用 httptest 编写单元测试以及在其定义中使用上下文的处理程序?

一段时间以来,我一直在尝试弄清楚如何为使用上下文作为其定义的一部分的处理程序编写单元测试。例子funcHandler(ctxcontext.Context,whttp.ResponseWriter,r*http.Request)经过一番谷歌搜索后,我发现了这个article这使得它看起来像//copiedrightfromthearticlerr:=httptest.NewRecorder()//e.g.funcGetUsersHandler(ctxcontext.Context,whttp.ResponseWriter,r*http.Request)handler:=http.Han

unit-testing - 需要文件的单元测试方法

我有以下功能,我需要为它创建一个单元测试packagemainimport("fmt""io/ioutil""os")typeSourcestruct{Pathstring}typefileReaderinterface{readOneFile()([]byte,error)}func(sSource)readOneFile()([]byte,error){cwd,err:=os.Getwd()file,err:=ioutil.ReadFile(fmt.Sprintf("%s/file.txt",cwd))iferr!=nil{returnnil,fmt.Errorf("errorea