草庐IT

integration-testing-mongodb-sprin

全部标签

unit-testing - 在 go api 调用中测试

我想为我的api编写测试用例,例如:数据库查询失败、代码错误、响应错误等。所以我在Controller文件夹中创建了一个名为controller_test.go的文件。这是我的代码:packagecontrollersimport("net/http""testing""net/http/httptest")func(imcImessageSoundController)TestHealthCheckHandler(t*testing.T){req,err:=http.NewRequest("GET","sound/imessage_sound",nil)iferr!=nil{t.Fa

mongodb - 如何通过管道函数获取子字段值

我正在编写代码,用于在golang中从mongodb接收数据。我的代码如下:typeDataContentstruct{Createtime.Time`bson:"create"`Descstring`bson:"desc"`}typeDatastruct{Idbson.ObjectId`bson:"_id,omitempty"`Descstring`bson:"desc"`ContentDataContent`bson:"content"`}funcget()error{result:=[]Data{}coll:=session.DB(“”).C(“aaa”)project:=bso

mongodb - 在出现错误的情况下重新创建 mgo session (读取 tcp 127.0.0.1 :46954->127. 0.0.1:27017: i/o 超时)

我想知道如何使用mgo在Go中管理MongoDBsession,尤其是关于如何正确确保session已关闭以及如何对写入失败使用react。我已阅读以下内容:BestpracticetomaintainamgosessionShouldIcopysessionforeachoperationinmgo?仍然不能将其应用于我的情况。我有两个goroutine,它们将事件一个接一个地存储到MongoDB中,共享同一个*mgo.Session,两者看起来基本上如下所示:funcstoreEvents(session*mgo.Session){session_copy:=session.Cop

mongodb - 在嵌入式数组 golang 中检索范围时间 mongodb 之间的值

这是我的mongodb数据库:"_id":ObjectId("58808d735ba19c2797f486ca"),"userid":ObjectId("58808d735ba19c2797f486c9"),"history":[{"floorId":"309cf96f-1812-44f6-8d94-d5ce2b8839be","time":ISODate("2017-01-19T09:57:34.572Z"),"position":{"latitude":48.815267598833806,"longitude":2.3630101271630677},"pointcoordina

unit-testing - 如何在向 API 发送 http 请求时进行模拟

我已经使用go-gin在Go中实现了一个ReSTAPI,我正在尝试测试一个如下所示的处理函数funceditNameHandler(c*gin.Context){//makeaReSTcalltoanotherservercallToAnotherServer()c.Status(200)}我想模拟callToAnotherServer方法,以便我的测试用例根本不调用第3方服务器。我的测试用例看起来像funcTestSeriveIdStatusRestorePatch(t*testing.T){//Requestbodysend:=strings.NewReader(`{"name":

unit-testing - 我如何对文本将出现在屏幕中央进行单元测试?

这是一个小脚本。packagebashutilimport("fmt""github.com/nsf/termbox-go")funcCenter(sstring){iferr:=termbox.Init();err!=nil{panic(err)}w,_:=termbox.Size()termbox.Close()fmt.Printf(fmt.Sprintf("%%-%ds",w/2),fmt.Sprintf(fmt.Sprintf("%%%ds",w/2+len(s)/2),s),)}我可以对其进行单元测试吗?我怎样才能测试它?我认为测试一个片段这么少是胡说八道。但是,...如果我

testing - golang 单元测试 : inner function parameters

我在go函数中的工作流程很简单,但是当进行单元测试时,我卡在了将参数传递给内部函数,或者模拟内部函数返回结果。代码:packagemyFuncimport(myPackagebookPackage)funcInit()(errerror){err=getResource(myPackage.GetPath())...}funcgetResource(pathstring)(errerror){//getresourcefrompath...err:=bookPackage.GetBook(path)}测试:packagemyFuncimport"testing"funcTestInit

mongodb - $lookup 和 $match Mongodb golang

我想通过在MongoDB上使用$lookup和$match来获取带有外键的文档。有一个“Jobs”集合,用于存储Job文档。在作业文档中有两个字段用作外键“creatorParent”和“Children”。CreatorParent是“Users”集合的外键,Children数组包含用户child的ID。当我列出所有作业时,我想从“用户”集合中检索CreatorParentID和ChildrenID的详细信息。我想用ParentDetail和ChildDetail编码“工作”文档。我不想为此编写自定义方法。是否可以使用MongoDB查询来处理它?顺便说一下,我是MongoDB的初学者

testing - 如何在单元测试中测试函数的输出 (stdout/stderr)

我有一个简单的函数要测试:func(t*Thing)print(min_verbosityint,messagestring){ift.verbosity>=minv{fmt.Print(message)}}但是我怎样才能测试函数实际发送到标准输出的内容呢?Test::Output在Perl中做我想做的事。我知道我可以编写自己的所有样板文件以在Go中执行相同的操作(如here所述):orig=os.Stdoutr,w,_=os.Pipe()thing.print("Somemessage")varbufbytes.Bufferio.Copy(&buf,r)w.Close()os.Std

mongodb - 如何使用 Go 更新 Mongodb 中的数组字段

我想把stu1改成stu3import("gopkg.in/mgo.v2""gopkg.in/mgo.v2/bson")typeStudentstruct{Namestring`bson:"name"`Agestring`bson:"age"`}typeClassstruct{Idstring`bson:"_id"`Student[]Student`bson:"student"`}col:=mongosession.DB("test").C("class")stu1:=Student{"jack","18"}stu2:=Student{"rose","16"}class:=Class{