草庐IT

test_nil_is_an_object

全部标签

testing - 去测试 : clean up after panic

假设我为我的测试设置了一个带有数据库的Docker容器,我在testing.TestMain中这样做了因为我希望这一次在全局范围内完成。我在此testing.Main()中编写了一个defer语句来执行清理(即删除DB容器)。现在,假设出了点问题,我的测试崩溃了。Thisissue告诉我我无法编写自定义recover代码来确保删除容器。这是真的:testing.M.Run()执行它自己的recover()调用,看起来没有办法覆盖它的行为。问题是:我应该怎么做才能确保无论如何执行我的清理代码? 最佳答案 如您链接到的问题中所述:The

unit-testing - 在 Golang 中测试接受不返回值的回调函数的方法

我正在尝试测试以下功能://SendRequestAsyncsendsrequestasynchronously,acceptscallback//func,whichitinvokes////Parameters://-`context`:somecontext//-`token`:sometoken//-`apiURL`:theURLtohit//-`callType`:thetypeofrequesttomake.Thisshouldbeoneof//theHTTPverbs(`"GET"`,`"POST"`,`"PUT"`,`"DELETE"`,...)//-`callBack

sqlite - 运行时错误 : invalid memory address or nil pointer dereference when Querying

我一直在研究用于身份验证的API,在尝试将其部署到服务器时,我偶然发现了这个奇怪的错误。该代码在我的笔记本电脑上运行得非常好,但是当我尝试部署它时,出现了这个错误:PANIC:runtimeerror:invalidmemoryaddressornilpointerdereferencegoroutine21[running]:github.com/urfave/negroni.(*Recovery).ServeHTTP.func1(0x7f5771b811e8,0xc4200980e8,0xc42009a870,0xc420138800)/home/linux/go/src/gith

unit-testing - 如何编写When a function having the parameters of c *gin.Context 的测试用例

我正在用golang为我的项目编写Controller的测试用例。在Controller中有函数名称SaveProvider()有参数c*gin.Context我不知道如何将JSON传递给c*gin.Context这个参数以及我如何测试我在Controller中使用的函数谁能告诉我这段代码中的问题是什么。它也称为表驱动测试。packagecontrollersimport("bkapiv1/models""fmt""testing""github.com/gin-gonic/gin")funcTestSaveProvider(t*testing.T){typeargsstruct{c*

go - 程序在主程序 block 中发现错误后出现 panic 。 panic : runtime error: invalid memory address or nil pointer dereference

我是golang的新手。在定义位置后trycatch主block中的错误后,我的程序出现panic。我在某处读过,添加defer.close()可能会有所帮助,但编译器再次说你的结构中不存在这样的定义帮助我解决它。typeIPInfostruct{IPstringHostnamestringCitystringCountrystringLocstringOrgstringPostalstring}funcmain(){ip:=getLocalIpv4()location,err:=ForeignIP(ip)iferr!=nil{fmt.Println("errorbro")}fmt.P

golang test spy 错误地比较相等性

我正在学习围棋并正在改编来自testdouble的Java生命游戏示例.然而,我编写的测试spy错误地比较了我的World结构的相等性——测试在它应该失败的时候通过了,因为output(world)没有被调用。我做错了什么?测试:packagegameoflifeimport("testing""github.com/google/go-cmp/cmp")funcTestZeroGenerations(t*testing.T){generatesSeedWorldStub:=GeneratesSeedWorldStub{}outputsWorldSpy:=OutputsWorldSpy

go - 使用beego验证码: invalid memory address or nil pointer dereference

我想在Beego下使用captcha生成验证码。但是它有错误无效的内存地址或零指针取消引用。有谁知道如何解决这个问题?谢谢。RequestMethod:GETRequestURL:/accounts/forgotpasswordRemoteAddr:127.0.0.1StackC:/Go/src/runtime/asm_amd64.s:573C:/Go/src/runtime/panic.go:505C:/Go/src/text/template/exec.go:137C:/Go/src/runtime/asm_amd64.s:573C:/Go/src/runtime/panic.go

go - 无效的内存地址或 nil 指针与 mgo 取消引用

packagemainimport("encoding/json""fmt""io/ioutil""net/http""github.com/gorilla/handlers""github.com/gorilla/mux""gopkg.in/mgo.v2")typeDataIgstruct{Memberstring`json:"Member"`Timestampfloat64`json:"Timestamp"`Namestring`json:"Name"`Bidstring`json:"Bid"`Offerstring`json:"Offer"`Changestring`json:"

unit-testing - 如何在 Golang 中实现 stub ? stub 和模拟之间有什么区别?

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭4年前。Improvethisquestion我在Golang的单元测试中使用模拟。但是在Golang的实现代码中如何区分stub和mock呢?

unit-testing - 为期望按下的键继续的函数编写 Golang 单元测试

我在Golang中有一个这样的函数:funcExit(codeint){.........keyboard.Open()deferkeyboard.Close()keyboard.GetKey()PrintAndLogInfo("\nBye.")os.Exit(code)}我正在使用以下库来使用此键盘功能github.com/eiannone/keyboard我想为此功能编写单元测试。但是,keyboard.GetKey()需要按下一个键。也许正因为如此,当我运行调用函数Exit的测试时,它失败并显示错误:panic:函数GetKey()应该在Open()之后调用[已恢复]panic: