草庐IT

using-transactions-for-unit-tests

全部标签

unit-testing - 在 Go 单元测试中使用两个不同的模拟

我正在学习使用gomock的mockgen实用程序第一次在Go中使用mock进行单元测试。我的单元测试工作正常,除了其中一个。被测方法有两个依赖项:一个依赖于数据库,另一个依赖于它调用restapi的外部服务。数据库的模拟(mockRepo)工作正常,因为被测方法正确调用模拟而不是实际的repo代码。然而,rest客户端的mock继续调用实际的rest客户端而不是mock代码。我不知道为什么。有人可以解释原因并帮助修复吗?这是我的单元测试:funcTestService_CreateWorkspace(t*testing.T){ts:=NewTestService(t)deferts.

go: var declared but not used 错误 - 如何解决?

在这个函数中,我得到了“sdeclaredandnotused”,我不明白-我是否需要以某种方式将其标记为“我真的使用过它”还是什么?funcgetString(datamap[string]interface{},namestring)(string,error){s:=data[name]ifreflect.TypeOf(s).Kind()!=reflect.String{returns.(string),nil}return"",&apiError{1,"it'snotastring"}}奇怪的是,我没有从这个函数中得到错误:funcgetInt(datamap[string]i

dictionary - 戈朗 : Using a defined interface in a Map Value

在链接的Playground示例中,我定义了一个类型:typeDoMapmap[int]func(Doer)stringDoer在我定义的接口(interface)类型中。我在具体类型MyDoer上实现接口(interface)。我希望能够构造一个DoMap,其中该映射中的条目包含如下两个函数:func(Doer)string//thisworksfunc(*MyDoer)string//thisdoesn'tfunc(MyDoer)string//more-or-lessthesameidea,alsodoesn't我不能,虽然很明显它们是不同的类型,但我想知道为什么我不能,因为函数

arrays - Append for array of maps raplaces all previous array items on the 最新的一个

这个问题听起来可能很愚蠢,但我真的不明白哪里出了问题。我想像这样创建一个map数组:values:=make([]map[string]string,0)然后我创建一些map:row:=make(map[string]string)row["item1"]="value1"row["item2"]="value2"然后将其追加到数组中:values=append(values,row)打印值现在给出:[map[item1:value1item2:value2]]使用其他一些值做同样的事情:row["item1"]="value3"row["item2"]="value4"values=

go - vendor 问题 : found packages text (doc. 去)和转换(examples_test.go)在我的 vendor

这个问题在这里已经有了答案:Error"can'tloadpackage:packagemy_prog:foundpackagesmy_progandmain"(3个答案)关闭4年前。我遇到了一些问题:enterimagedescriptionhere..\vendor\github.com\spf13\afero\util.go:28:2:在D:\golang\src\services\vendor中找到包文本(doc.go)和转换(examples_test.go)\golang.org\x\text\transform我该如何解决这个问题?谢谢。

GoSonar : how to generate go test -json > report. json

如何生成gotest-json>report.jsonGo语言版本:Go1.10.3SonarQube属性:sonar.go.tests.reportPaths=report.json官方Sonar文档->https://docs.sonarqube.org/display/PLUG/Unit+Tests+Results+Import生成文件.PHONY:testtest:@$(foreachpackage,$(packages),\gotest-coverprofile$(package)/cover.out-covermode=count$(package);).PHONY:cov

unit-testing - 如何测试以确保函数被调用?

我想做单元测试。为了简单起见,我只想确保调用“JWTCheck”。我该怎么做?这是我实现JWTCheck的文件:typeJWTCheckerstruct{SubjectPrefixstring}func(j*JWTChecker)JWTCheck(nexthttp.Handler)http.Handler{//Dosomething}这是我实现路由器的地方:import("net/http""github.com/gorilla/mux")//Routerreturnsapreconfiguredrouterforapplicationfunc(a*Adapter)Router()ht

go - "Cannot use myType as type interface{}"?我以为在 Go 中所有类型都算作 interface{}?

这个问题在这里已经有了答案:Convert[]stringto[]interface{}[duplicate](3个答案)Convertingsliceofstructstosliceofemptyinterface[duplicate](1个回答)Whycan'tIsubstituteasliceofonetypeforanotherinGo?(3个答案)Whycan'tIpassa`func()[]int`as`func()[]interface{}`ingo?(2个答案)Whyaslice[]structdoesn'tbehavesameas[]builtin?(3个答案)关闭4

gorequest 包 : check specifically for timeout

我正在使用以下包发出出站http请求https://github.com/parnurzeal/gorequest例如,我正在发出如下所示的GET请求res,body,errs=goReq.Get(url).End()我的问题是如何判断请求是否超时。 最佳答案 由于超时方法setsthedealinesfordial,read,andwrite,你可以使用os.IsTimeout(net和net/url包中的所有错误类型都实现了Timeout()bool)。gorequest不支持上下文,所以context.Canceled不必考虑

unit-testing - 如何确保所有异步函数在执行 defer 之前运行

我有一个函数说Myfunction(){x.RunAsync()//AsyncFunctioncall}我必须为Myfunction()编写一个测试用例。我正在为此使用go-mock。还有mockedx。Test_MyFunction(){mockCtrl:=gomock.NewController(t)defermockCtrl.Finish()//EXPECT()calltostubRunAsync()}现在的问题是我的测试运行成功,但不知何故最后它panic地说,对mockX.RunAsync()的调用丢失了。我认为这是因为在我的RunAsync被stub之前正在执行延迟。如何确