草庐IT

Quiz_for_Unit

全部标签

unit-testing - 仅测试文件中的掩码包名称

为了追求100%的单元测试覆盖率,我们尝试在一个函数中测试多行代码。相关函数调用运行时包://functionNamereturnsastringrepresentingthefunctionnameofthefunctionnstackframesabovethecaller.//ifn=0,thenameofthefunctioncallingfunctionName()willbereturned.funcfunctionName(nint)string{pc,_,_,ok:=runtime.Caller(n+1)if!ok{return"unknownfunction"}me:

unit-testing - 在 Go 中进行单元测试时,是否可以动态断言两个值是否相等?

我刚刚开始使用Go。我正在编写单元测试,我希望能够使用表格进行测试,其中要与实际结果进行比较的结果有时应该或不应该相等。例如,这是我目前拥有的代码:packagemainimport("github.com/stretchr/testify/assert""testing")funcTestFunc(t*testing.T){tables:=[]struct{inputstringcomparisonResultstringshouldBeEqualbool}{{"someirrelevantinput","someresult",true,},{"someotherirrelevan

高语 : "No common algorithm for key exchange" error

我是GoLang的新手,我正在尝试使用go连接到远程服务器。但是我不断收到以下错误Failedtodial:ssh:handshakefailed:ssh:nocommonalgorithmforkeyexchange;clientoffered:[curve2****-sh****@libssh.org****-sha*-nis****ecdh-sha2-nistp384ecdh-sha2-nistp****diffie-hellman-group14-sha1diffie-hellman-group1-sha1],serveroffered:[diffie-hellman-grou

for-loop - golang 初始化后得到空 slice

我有3个结构:Queue、Config、TaskertypeQueuestruct{NamestringConcurrentintConnections[]*redis.Client}typeConfigstruct{Queues[]QueueRedisAddrstringRedisDBint}typeTaskerstruct{ConfigConfig}问题发生在这个方法中,我在for循环中初始化了queue.Connections,但是我在for循环之外得到了零长度的queue.Connectionsfunc(t*Tasker)StartListening(){forj:=0;j这是

unit-testing - 如何模拟/单元测试嵌套函数

我有一个函数在其他函数中被调用。send_api.gofunction*send_api*(client*http.Client,urlstring)map[string]string,error{//sendapirequestandparsetheresponseandreturnthedictreturndictmapforeg:{applefruit}}然后这个函数在ma​​in()函数中被调用func*main()*{getmap:=send_api(client*http.Client,"test.com")}good.gofunc*get_dict_key*(keystr

unit-testing - 结构字段还原

这个问题在这里已经有了答案:Howtosetandgetfieldsinstruct'smethod(3个答案)Assignanewvaluetoastructfield(2个答案)关闭5年前。我正在玩Go,但在做一些测试时发现了这种奇怪的情况。我正在使用结构中的方法将变量发送到另一个应该更改字段的方法,但是当我最后检查它时,该字段返回到第一个值,这让我感到困惑。func(thisTVManager)sendMessage(messagestring){fmt.Println("5",this.connector)payload:=map[string]string{"id":"0",

unit-testing - 如何验证在单独的 go 例程中调用了一个方法

在为一个方法编写单元测试时,我遇到了一个问题。首先是被测代码片段:funcMehodToBeTested(eEntity){gosaveAudit(e)//dosomethingonwhichassertionscanbedone}实体可以被模拟。在saveAudit方法中,调用了Entity.Save方法。在我的UT中,我想断言Entity.Save方法被调用一次。以下是我当前的UT:funcTestMethod(t*testing.T){varmock=&mockEntity{}mock.On("Save").Return(nil)//makecalltofuncundertest

unit-testing - 您将如何对一个唯一目的是进行数据库调用的方法进行单元测试?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭5年前。Improvethisquestion我有这样的方法:func(alert*Alert)CreateAlert(db*mgo.Database)error{returndb.C("alerts").Insert(&alert)}我应该如何对此进行单元测试?如果我只是调用此方法进行uni测试,那么我将不得不创建调用的测试数据库。我以某种方式觉得这将是集成测试的一部分,因为我们正在调用DB。如果我模拟db对象,那么它永远不会测试实

for-loop - 在 for 循环中使用 AtoI 时出错

我是golang的新手,我确信我遗漏了一些非常基本的东西,但由于我已经搜索了网络,但没有找到任何可以帮助我理解我的问题的东西,我想我会把它贴在这里。本质上我想做的是从字符串到int的简单转换。字符串作为表示为字符串的数值(即“1”)到达;我想将它更改为等效的int以便我可以在switch...案例中使用它。我已经尝试了Atoi和parseInt,但都因相同的错误而失败:./test.go:1765:cannotusev(type[]string)astypestringinargumenttostrconv.Atoi我试图先将它分配给一个字符串,然后将它传递给Atoi,但它仍然没有用。

go - 嵌套接口(interface) : X does not implement Y (Wrong type for Z method)

在一个包中我有一个接口(interface)Repository有一个方法GetReporter返回一个接口(interface)Reporter.这是由一个函数使用Execute这需要Repository并得到它的Reporter通过GetReporter功能。在另一个包中我有一个结构GithubRepository有一个方法GetReporter返回GithubReporter.在第三个包中,我想调用Execute使用GithubRepository在包#1外运行实例。我正在尝试让包1和包2彼此独立,而不是从另一个导入某些东西。第三个包应该结合前两个。Golang返回:cannot