草庐IT

package_test

全部标签

unit-testing - 处理程序单元测试的模拟功能

我被模拟测试困住了,这是我的处理程序路线:r.Handle("/users/{userID}",negroni.New(negroni.HandlerFunc(validateTokenMiddleware),negroni.Wrap(http.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){getUserDetailsHandler(w,r,db)})),)).Methods("GET")这是我的处理程序:funcgetUserDetailsHandler(whttp.ResponseWriter,r*http.Reques

testing - 在 Golang 的 TestMain() 中调用不同的测试函数

我正在尝试测试我的httpController并且我使用TestMain函数来准备我的测试,但是在我运行所有测试请求之前我需要先运行TestAuthUserController测试,它创建并授权用户.为此,我使用了wrapperfunc,它帮助我调用TestAuthUserController:funcTestMain(m*testing.M){//someprepearingstepsAuthUserController()//createandauthorizeuserbeforeallothertestsm.Run()fmt.Println("afterallinmain")db

go - 无法在 MacOS 上安装 Revel "package gopkg.in/fsnotify.v1: unrecognized import path"

按照“GettingStarted”方向:$goget-vgithub.com/revel/revelFetchinghttps://gopkg.in/fsnotify.v1?go-get=1Parsingmetatagsfromhttps://gopkg.in/fsnotify.v1?go-get=1(statuscode404)packagegopkg.in/fsnotify.v1:unrecognizedimportpath"gopkg.in/fsnotify.v1"(parsehttps://gopkg.in/fsnotify.v1?go-get=1:nogo-importme

unit-testing - 为从中读取的函数填充 os.Stdin

如何在我的测试中填充os.Stdin以获取使用扫描仪从中读取的函数?我使用以下函数通过扫描器请求用户命令行输入:funcuserInput()error{scanner:=bufio.NewScanner(os.Stdin)println("Whatisyourname?")scanner.Scan()username=scanner.Text()/*...*/}现在如何测试这种情况并模拟用户输入?以下示例不起作用。标准输入仍然是空的。funcTestUserInput(t*testing.T){varfile*os.Filefile.Write([]byte("Tom"))os.St

unit-testing - Gomega 的 Equal() 可以处理多个值吗?

我正在测试使用gomega向服务器发送模拟请求的功能,我想验证1.请求已开始2.请求已完成。为此,我返回两个bool值。在下面的示例中,它们都应评估为真,但值可能会发生变化。我试过这个:g:=gomega.NewGomegaWithT(t)...g.Eventually(func()(bool,bool){...start=falseend=trueif(request.status=="started"){start=true}if(request.status=="complete"){end=true}returnstart,end}).Should(Equal((true,tr

unit-testing - 需要帮助查明中间件测试(Golang)中零引用错误的原因

我在为我的http处理程序(特别是身份验证中间件)编写单元测试时遇到了“无效内存地址或nil指针取消引用”错误。这里是要测试的函数:funcAuthenticate(nexthttp.Handler)http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,req*http.Request){varerrerrorck,err:=req.Cookie("session")iferr!=nil{w.Header().Add("Authorization","expired/invalid")next.ServeHTTP(w

unit-testing - 单元测试 Google 的 Go API 客户端

我目前正在使用Google的APIgoclient使用Go编写工作流.我是Go的新手,在对客户的服务进行单元测试时遇到了麻烦。以下是在GoogleCloud项目中启用API的示例方法。func(gcloudService*GCloudService)EnableApi(projectIdstring,apiIdstring)error{service,err:=servicemanagement.New(gcloudService.Client)iferr!=nil{returnerr}requestBody:=&servicemanagement.EnableServiceReque

Go package wincred 在将密码从 []byte 转换为字符串时添加空格

我正在使用Windows凭据管理器通过wincredpackage为我在Go中构建的应用程序存储数据库凭据。它适用于检索由程序包本身创建的凭据的密码,但是对于直接通过Windows凭据管理器创建的凭据,程序包在从[]字节转换为字符串时在字符之间添加“空格”(字节“0”).//Retrieveacredentialobjectpackagemainimport("fmt""github.com/danieljoos/wincred")funcmain(){cred,err:=wincred.GetGenericCredential("myGoApplication")iferr==nil

go - 在 *testing.M 中记录一行

在以*testing.T或*testing.B为参数的测试函数中,很容易记录一行:funcTestMyFunc(t*testing.T){t.Log("hellotesting"...}如何在将*testing.M作为参数的设置函数中执行此操作?funcTestMain(m*testing.M){???} 最佳答案 查看packagetesting的文档显示类型testing.M没有与之关联的日志记录方法。您应该使用packagelog进行日志记录(正如评论中已经提到的)。 关于go-在

unit-testing - 使用 httptest 或 http 包模拟服务器故障

如何在隔离单元测试中使用httptest或http包模拟服务器故障?详细信息:我一直在使用gorillawebsockets,所以mt,msg,err:=t.conn.ReadMessage()mt值必须是-1当服务器宕机时。我尝试了以下作为主要选项:varsrv*httptest.Serversrv=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){_,err:=wsUpgrader.Upgrade(w,r,nil)iferr!=nil{t.Fatal(err)}srv.Clos