草庐IT

go - 模拟一个在 Golang Http 处理程序中调用的函数

我有一个函数需要测试,它看起来像这样:funcparmHandler(whttp.ResponseWriter,r*http.Request){...data,err=backenddb_call(r*http.Request)...return}functionbackenddb_call(r*http.Request)(data[]Data,errerror){parm:=r.URL.Query().Get(parm)//GetDatafromDBforparm...return}在这种HTTP处理程序的情况下,我无法修改parmHandler参数并添加帮助接口(interface

go - 如何模拟接口(interface)实现

我的界面.gotypeMyInterfaceinterface{fun1()stringfun2()intfun3()bool}funcFoo(miMyInterface)string{returnmi.fun1()}我的接口(interface)测试.gotypeMyInterfaceImplementationstruct{}func(miMyInterfaceImplementation)fun1()string{return"foobar"}func(miMyInterfaceImplementation)fun2()int{returnint(100)}func(miMyIn

go - 如何使用 testify/mock 在 golang 中模拟数据库层

我正在尝试在服务器上运行单元测试,并使用“github.com/strethr/testify/mock”模拟数据库层。将所有代码放在这里会使它变得非常困惑,所以我创建了一个小项目,它将给出我的代码结构的想法以及我真正想要实现的目标“https://github.com/utkarsh17ife/goMockPractice”。您可以提取此代码并运行“gotest./...”(不确定在这里提供github链接是否正确,但我觉得这会让事情变得更容易)Serverstruct有一个接口(interface)类型的字段db,所以当我们创建一个普通服务器时,db被分配给真实数据库,但在测试时它

go - Testify mock 正在返回函数未被调用的断言

我的测试一直失败,但没有实际调用发生,但我肯定func被调用了(这是一个日志函数,所以我在终端上看到日志)基本上我的代码看起来像这样:common/utils.gofuncLogNilValue(ctxstring){log.Logger.Warn(ctx)}main.goimport("common/utils")funcCheckFunc(*stringvalue){ctx:="Somecontextstring"ifvalue==nil{utils.LogNilValue(ctx)//voidfuncthatjustlogsthestring}}test.gotypeMyMock

oop - 通过golang中的接口(interface)模拟功能

我正在尝试编写一个单元测试代码形式,其代码具有如下3级函数调用:主函数调用函数A(),然后函数A根据某些条件调用函数B()和C(),函数B调用函数E()和F(),而函数C调用函数G()和H()在某些条件下。上面就像我开发的代码,这里我想为函数B模拟函数E()和F(),为函数C模拟G()和H()。请建议我如何使用接口(interface)来实现。 最佳答案 Abstractfunctiontype您可以通过依赖注入(inject)而不是使用接口(interface)来做到这一点:import("fmt""math")typeafunc

unit-testing - 包中的模拟方法

我正在寻找模拟单元测试的一些方法。不幸的是,代码的结构不是很好。varconfig=struct{abc*abc}funcInit(arg1){//config.abc=newAbc(arg2,arg3)}funcUnitTestThis(){//somecodehereconfig.abc.Search(arg4,arg5)//codehere}如何对UnitTestThis函数进行单元测试,模拟Search方法的结果?我一直在尝试创建一个接口(interface)并模拟这些方法,但未能成功。 最佳答案 如果config.abc字

unit-testing - 如何在 Golang 中模拟结构方法

我正在阅读这个页面(我没有使用亚马逊,只是为了golang教育而阅读)https://aws.amazon.com/blogs/developer/mocking-out-then-aws-sdk-for-go-for-unit-testing/当我自己尝试时,我遇到了类型错误。typeQueuestruct{ClientThirdPartyStructURLstring}typemockedReceiveMsgsstruct{ThirdPartyStructRespValueIWantToMock}q:=Queue{Client:mockedReceiveMsgs{}}当我尝试做完全

unit-testing - 创建模拟函数

您好,我想测试或模拟某个函数并为此返回模拟响应。下面演示的是我的代码示例.gopackagemainimport("fmt"log"github.com/sirupsen/logrus")varconnectDB=ConnectfuncSample(){config:=NewConfig()response:=connectDB(config)fmt.Println(response)log.Info(response)}funcConnect(config*Config)string{return"Insidetheconnect"}我的测试是这样的Sample_test.gopac

unit-testing - golang 中用于测试的模拟函数

我想通过模拟其他包(package2)中的FetchAllData()和SaveData()为CreateData()函数编写单元测试用例,请帮助我用示例模拟该函数,提前致谢funcCreateData(inputpackage1.InputRequest)(outputpackage1.OututResponse){..somecode..somecodeDBdata,err:=package2.FetchAllData()//functiontofetchdatafromdatabase..somecode..somecodeid,insertErr:=package2.SaveD

go - 在 Go 包函数中模拟函数

我正在尝试模拟在我的Go代码中的API函数调用中使用的HTTP客户端。import("internal.repo/[...]/http""encoding/json""strings""github.com/stretchr/testify/require")funcCreateResource(t*testing.T,urlstring,bodyReqinterface{},usernamestring,passwordstring,resourcestring)[]byte{bodyReqJSON,err:=json.Marshal(bodyReq)iferr!=nil{panic