草庐IT

test_phoenix

全部标签

unit-testing - 在单元测试中模拟 context.Done()

我有一个HTTP处理程序,它为每个请求设置上下文截止时间:funcsubmitHandler(streamchandata)http.HandlerFunc{returnfunc(whttp.ResponseWriter,r*http.Request){ctx,cancel:=context.WithTimeout(r.Context(),5*time.Second)defercancel()//readrequestbody,etc.select{casestream我很容易就能测试http.StatusNoContentheader,但我不确定如何测试select语句中的大小写。在

unit-testing - 在单元测试中模拟 context.Done()

我有一个HTTP处理程序,它为每个请求设置上下文截止时间:funcsubmitHandler(streamchandata)http.HandlerFunc{returnfunc(whttp.ResponseWriter,r*http.Request){ctx,cancel:=context.WithTimeout(r.Context(),5*time.Second)defercancel()//readrequestbody,etc.select{casestream我很容易就能测试http.StatusNoContentheader,但我不确定如何测试select语句中的大小写。在

testing - 如何在 Go 中测试 http.NewRequest?

我最熟悉Go中的测试,但正在努力寻找一种方法来测试以下函数:funcpostJson(urlstring,jsonData[]byte)error{req,err:=http.NewRequest("POST",url,bytes.NewBuffer(jsonData))iferr!=nil{returnerr}req.Header.Set("Content-Type","application/json")client:=&http.Client{}resp,err:=client.Do(req)iferr!=nil{returnerr}resp.Body.Close()return

testing - 如何在 Go 中测试 http.NewRequest?

我最熟悉Go中的测试,但正在努力寻找一种方法来测试以下函数:funcpostJson(urlstring,jsonData[]byte)error{req,err:=http.NewRequest("POST",url,bytes.NewBuffer(jsonData))iferr!=nil{returnerr}req.Header.Set("Content-Type","application/json")client:=&http.Client{}resp,err:=client.Do(req)iferr!=nil{returnerr}resp.Body.Close()return

testing - go中函数的正确测试方法

我刚开始接触测试。我的方法返回一个带有md5散列的[]byte。funcmyHash(sstring)[]byte{h:=md5.New()io.WriteString(h,s)returnh.Sum(nil)}它工作正常,哈希值看起来不错,但是当我用这种方法测试它时:funcTestMyHash(t*testing.T){s:="linux"bf:=("e206a54e97690cce50cc872dd70ee896")x:=hashor(s)if!bytes.Equal(x,[]byte(bf)){t.Errorf("myHash...")}}它总是会失败。首先,我认为将字符串转换

testing - go中函数的正确测试方法

我刚开始接触测试。我的方法返回一个带有md5散列的[]byte。funcmyHash(sstring)[]byte{h:=md5.New()io.WriteString(h,s)returnh.Sum(nil)}它工作正常,哈希值看起来不错,但是当我用这种方法测试它时:funcTestMyHash(t*testing.T){s:="linux"bf:=("e206a54e97690cce50cc872dd70ee896")x:=hashor(s)if!bytes.Equal(x,[]byte(bf)){t.Errorf("myHash...")}}它总是会失败。首先,我认为将字符串转换

testing - 在 Go 中模拟单个方法

在Go中,如何模拟一个接口(interface)而不必实现每个方法?假设我有一个Car接口(interface)和一个实现该接口(interface)的Corolla结构:typeCarinterface{changeTire()startEngine()....refuel()}typeCorollastruct{...}func(cCorolla)changeTire(){...}func(cCorolla)startEngine(){...}func(cCorolla)refuel(){...}假设我还有一个依赖于Car的Garage结构:typeGaragestruct{MyC

testing - 在 Go 中模拟单个方法

在Go中,如何模拟一个接口(interface)而不必实现每个方法?假设我有一个Car接口(interface)和一个实现该接口(interface)的Corolla结构:typeCarinterface{changeTire()startEngine()....refuel()}typeCorollastruct{...}func(cCorolla)changeTire(){...}func(cCorolla)startEngine(){...}func(cCorolla)refuel(){...}假设我还有一个依赖于Car的Garage结构:typeGaragestruct{MyC

unit-testing - 不运行名称为 package_test.go 的程序

我在包pack1下有以下代码。文件名是pack1.gopackagepack1varPack1Intint=42varpack1Float=3.14funcReturnStr()string{return"Helloworld!"}以及主程序中的以下代码。文件名是package_test.gopackagemainimport("fmt""./pack1")funcmain(){vartest1stringtest1=pack1.ReturnStr()fmt.Printf("Returnstringfrompack1:%s\n",test1)fmt.Printf("Integerfro

unit-testing - 不运行名称为 package_test.go 的程序

我在包pack1下有以下代码。文件名是pack1.gopackagepack1varPack1Intint=42varpack1Float=3.14funcReturnStr()string{return"Helloworld!"}以及主程序中的以下代码。文件名是package_test.gopackagemainimport("fmt""./pack1")funcmain(){vartest1stringtest1=pack1.ReturnStr()fmt.Printf("Returnstringfrompack1:%s\n",test1)fmt.Printf("Integerfro