草庐IT

android-testing

全部标签

testing - 如何在 GO 中测试响应体?

我有以下功能:funcSomeFunction(whttp.ResponseWriter,...)error{...returnjson.NewEncoder(w).Encode(&c)}其中c是我的结构。当我运行该函数时,我收到带有json的响应主体,但我无法测试该函数。例如,我有:funcTestSomeFunction(t*testing.T){...w:=httptest.ResponseRecorder{}err:=SomeFunction(w,...)..}在这种情况下,我的w.Body是空的。问题不在于我的功能,因为我在运行时得到了body。我认为我没有以正确的方式进行测

unit-testing - 单元测试 os.File.Write 调用

我想对调用os.File.Write()的函数进行单元测试,并希望达到100%的覆盖率。此函数返回n和一个错误。引发错误很容易。我只需要关闭文件。我怎样才能不引起写入错误和写入数据长度的值n不同?看起来我应该创建一个虚拟的os.File,我可以在上面控制返回的错误。不幸的是,os.File不是一个接口(interface)。编辑:根据PeterOS的回答,仔细检查文档后,Write()方法,是否为io.Writer或io.File如果err为nil,将始终返回写入的slice的长度。结果,看来我的问题毫无意义。我学到了一些重要的东西,谢谢。我有一些代码要清理。附带说明一下,我对100%

android - 如何将 Golang 和 Android 与 socket.io 连接起来?

我想使用socket.io将数据从Android发送到Golang。我用Nodejs正确地做到了但是现在,我想用Go来做。我找不到简单的示例。我该怎么做? 最佳答案 我假设您想要使用Socket.IO服务器库的Go实现,而不是标准的Node.js。如果是这样,您可以尝试googollee/go-socket.ioproject.这是项目页面中的示例:packagemainimport("log""net/http""github.com/googollee/go-socket.io")funcmain(){server,err:=s

unit-testing - 尝试在 TCP 监听器上提供服务时,由于超时,Go 测试模棱两可地失败

我有这个单元测试:funcTestServer(t*testing.T){db:=prepareDBConn(t)deferdb.Close()lis:=bufconn.Listen(1024*1024)t.Logf("Openedlistener:%v",lis)grpcServer:=grpc.NewServer(withUnaryInterceptor(),)t.Logf("Openedgrpcserver:%v",grpcServer)signKey:=getSignKey()ifsignKey==nil{t.Fatal("FailedtofindorparseRSApriva

testing - 如何提高 Go 中的测试覆盖率?

我正在做一些测试。我有一个文件dao.go:packagemodel_daoimport"io/ioutil"constfileExtension=".txt"typePagestruct{TitlestringBody[]byte}func(pPage)SaveAsFile()(eerror){p.Title=p.Title+fileExtensionreturnioutil.WriteFile(p.Title,p.Body,0600)}funcLoadFromFile(titlestring)(*Page,error){fileName:=title+fileExtensionbo

unit-testing - 使用 PanicsWithValue 进行 Golang 单元测试

我们正在尝试测试引发indexoutofrange错误的函数。单元测试的代码很简单,大概是这样的:import("testing""github.com/stretchr/testify/assert")funcTestIndexOutOfRange(t*testing.T){assert.PanicsWithValue(t,"indexoutofrange",func(){indexOutOfRange(9)})}但不幸的是测试失败并出现奇怪的错误===RUNTestIndexOutOfRange---FAIL:TestIndexOutOfRange(0.00s):1:ErrorTr

unit-testing - 如何测试在 golang 中打印到控制台的方法?

我有下一个结构。packageloggerimport"fmt"typeIPrinterinterface{Print(valuestring)}typeConsolePrinterstruct{}func(cp*ConsolePrinter)Print(valuestring){fmt.Printf("thisisvalue:%s",value)}测试范围说我需要测试ConsolePrinterPrint方法。如何覆盖这个方法?谢谢。 最佳答案 根据@icza写的评论,我在下面编写了测试。funcTestPrint(t*testi

android - 创建应用程序时如何将电话号码链接到图像?

我是新手,我正在使用gomobile创建一个应用程序。我想添加一张图片并将电话号码链接到该图片。关于如何做的任何提示?我不写代码,但如果提供示例,我也许能弄明白?谢谢! 最佳答案 你可以将一个按钮链接到一个图像,你可以在按钮上写下你可以在java中使用gettext函数获取的电话号码 关于android-创建应用程序时如何将电话号码链接到图像?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/ques

unit-testing - golang 这应该是一个集成测试吗?

我有一个实现数据库接口(interface)的方法,该方法将一个“对象”插入到数据库中。typedatabaseinterface{createLog(logDoc)(bool,error)}typemongostruct{databasestringcollectionstring}func(mmongo)createLog(llogDoc)(bool,error){s,err:=mgo.Dial("mongo")defers.Close()iferr!=nil{returnfalse,err}err=s.DB(m.database).C(m.collection).Insert(l

unit-testing - 安装go lang后无法运行go test程序

尝试运行用Go编写的测试时出现以下错误。我安装了Golang和dep。我是Go的新手,我不确定这里的问题是什么。有人可以帮助我吗?xxxx-dxxxx:testxxxx$gotest#_/Users/xxxx/dev/xxxx/test/xxxx/testapplication_cluster_test.go:10:2:cannotfindpackage"github.com/stretchr/testify/assert"inanyof:/usr/local/Cellar/go/1.10.3/libexec/src/github.com/stretchr/testify/assert