草庐IT

模拟式

全部标签

go - 在golang中从另一个内部调用的模拟函数

我正在尝试stubos.Stat和ioutil.ReadFile(path)使用下面的代码或者如果你喜欢这里在goplayground[1]packagemainimport("fmt""io/ioutil""os""strings")funcAssignFileValueFrom(pathstring,val*string){var(tempValue[]byteerrerror)if_,err=os.Stat(path);err==nil{iferr!=nil{fmt.Println("Therewasaosstaterror:",err)}tempValue,err=ioutil

go - 作证模拟函数在函数内部返回

我想模拟一个函数的响应。但是这个函数位于或在另一个函数内部调用。假设我有这个功能//main.gofuncTheFunction()int{//Somecodeval:=ToMockResponse()returnval}funcToMockResponse()int{return123}现在在我的测试文件上//main_test.gofuncTestTheFunction(t*testing.T){mockInstance=new(randomMock)mockInstance.On("ToMockResponse").Return(456)returned:=TheFunction

unit-testing - 如何为单元测试模拟标准包函数

我有三个功能:funcIsSymlinks(pathstring){......}func(c*MyClass)myFunc1(pathstring){...morecode...morecodeifIsSymlinks(path){realPath:=filepath.EvalSymlinks(path)}...morecode...morecode}funcmyFunc2(pathstring){...morecode...morecodeifIsSymlinks(path){realPath:=filepath.EvalSymlinks(path)}...morecode...m

go - 如何从 Visual Studio Code 中的包覆盖范围中排除生成的模拟

我正在通过VisualStudioCode对Golang项目进行单元测试,我使用的依赖项之一称为mockery.它通过在找到的包中创建目标Golang接口(interface)的模拟来工作。因此,即使我发誓要测试所述包中的所有内容,我的测试覆盖率也只有58%。然而,当我从包中取出模拟(事实证明我不需要它)时,我的测试覆盖率肯定像雨一样猛增到我预期的97.9%。有什么方法可以告诉VisualStudioCode“嘿,不要在包覆盖率计算中包含模拟模拟(以mock_开头)”? 最佳答案 解决方案是使用mockery-inpkg和-test

go - 测试中的模拟方法

我有一个要测试的类:typeApiGatewaystruct{usernamestringpasswordstringscsClient*scsClient.APIDocumentationauthAuth}typeAuthstruct{tokenstringvalidToint32}funcNew(hoststring,schemestring,usernamestring,passwordstring)*ApiGateway{varconfig=scsClient.TransportConfig{host,"/",[]string{scheme}}varclient=scsClie

go - 在 golang 中模拟内部函数

我想使用接口(interface)模拟函数,我能够模拟第一个函数callsomething在icza的大力帮助下,现在有点棘手了。我想测试函数vl1使用mockforfunctionfunction1,它是如何完成的。https://play.golang.org/p/w367IOjADFV主要包import("fmt""time""testing")typevInterfaceinterface{function1()bool}typemStructstruct{infostringtimetime.Time}func(s*mStruct)function1()bool{return

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

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

go - 如何在 golang 中模拟 GCP 的存储?

我真的不熟悉在go中模拟第三方库,我现在正在模拟cloud.google.com/go/storage我正在使用mockery.这是我当前的界面://ClientstorageclienttypeClientinterface{Bucket(namestring)BucketHandleBuckets(ctxcontext.Context,projectIDstring)BucketIterator}//BucketHandlestorage'sBucketHandletypeBucketHandleinterface{Attrs(context.Context)(*storage.B

go - 我如何模拟接口(interface)内的特定嵌入式方法

我有这段代码,我想为更新功能编写一个单元测试。我如何模拟FindByUsername函数?我尝试覆盖u.FindByUsername但它不起作用。另外,我可以编写一些函数来将u*UserLogic和userNamestring作为输入参数并执行u.FindByUsername()和模拟这个函数,但这不是一个干净的解决方案我需要一个更好的解决方案来模拟UserOperation接口(interface)中的方法。packagelogicimport("errors""fmt")var(dataStore=map[string]*User{"optic":&User{Username:"b

go - 如何检查 Cloud Pub/Sub 模拟器是否已启动并正在运行?

我有GC功能,我使用CloudPub/Sub模拟器在本地开发和测试这些功能。我希望能够从Go代码中检查CloudPub/Sub模拟器是否已启动并正在运行。如果没有,我想通知开发人员他/她应该在他/她在本地执行代码之前启动模拟器。当模拟器启动时我注意到一条线INFO:Serverstarted,listeningon8085也许我可以检查端口是否可用或类似。 最佳答案 我猜你用过这个命令:gcloudbetaemulatorspubsubstart你得到了以下输出:[pubsub]ThisistheGooglePub/Subfake.