草庐IT

模拟IC

全部标签

testing - go中接口(interface)模拟的方法

如果我有一个如下所示的handlerfunc。“模拟”或注入(inject)包装某些对象以进行测试的接口(interface)的最佳方法是什么?funcGetOrCreateUser(whttp.ResponseWriter,r*http.Request){user:=GetUserFromContext(r.Context())ifcreated:=user.GetOrCreate();created{smtp.SendEmail(...)return}else{w.Write([]byte("HelloUser!"))}}我遇到的唯一方法似乎是这样做:typeMailerinter

testing - 如何模拟 DNS 请求? (米克/dns)

我有一小段代码来解释我要测试的代码库。我已经跳过检查错误以使问题简短。funclastCNAME(domainstring)(lastCNAMEstring){ns:="8.8.8.8:53"c:=dns.Client{}m:=dns.Msg{}m.SetQuestion(domain,dns.TypeA)r,_,_:=c.Exchange(&m,ns)//LastCNAMEfor_,ans:=ranger.Answer{cname,ok:=ans.(*dns.CNAME)ifok{lastCNAME=cname.Target}}returnlastCNAME}模拟对名称服务器8.8.

unit-testing - 如何使用结构/接口(interface)来模拟依赖项以进行测试

我是新手...我的目标是单元测试我的ready()中的状态是否正在更新。我一直在看https://engineering.aircto.com/writing-testable-code-in-golang/并尝试找出如何使他们正在做的事情适应我的用例,尽可能填补golang知识的空白。我收到错误消息cannotusefakeSession(type*FakeSession)astype*discordgo.Sessioninargumenttoready但我不确定为什么我'我收到此错误。ma​​in.goimport("fmt""os""os/signal""syscall""git

go - 在 Go 测试包中模拟 statsd 客户端

我使用thisstatsdpackage将指标发送到我们的statsd服务器。为了初始化客户端,我在我的main中调用了一个metrics.Setup()来执行初始化。这个包看起来像这样:包:packagemetricsimport("fmt""github.com/cactus/go-statsd-client/statsd")//ClientcanbeusedtosendstatstovarClientStatsdAccess//SetupinitialisesmetricsgatheringfuncSetup(){ifClient==nil{prefix:=fmt.Sprintf

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