草庐IT

test_map

全部标签

unit-testing - 去单元测试找不到所需的文件

我在go中编写了一个单元测试,同一文件的init方法在项目的根目录中打开了一个文件。我遇到的问题是,当我运行测试时,测试包是不包含所需文件的根目录。如何告诉测试函数查看文件而不在测试文件的目录中创建重复文件?文件结构:main.go|+-helpers|+-data.go|+-data_test.gorequired_file.txt测试命令:gotestgithub.com/testproj/helpersdata.go中的代码:funcinit(){file,err:=os.Open("required_file.txt")iferr!=nil{log.Fatal(err)

unit-testing - 使 ioutil.ReadAll(response.Body) 在 golang 中抛出错误

出于某种原因,我似乎无法获取ioutil.ReadAll(res.Body),其中res是*http.Response由res,err:=hc.Do(redirectRequest)返回(对于hchttp.Client,redirectRequest*http.Request)。到目前为止的测试策略任何时候我在SUT中看到hc.Do或http.Request,我的直觉是启动一个假服务器并指向适当的申请说明。这样的服务器,对于这个测试,看起来像这样:badServer:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWrit

go - 在 TestMain 中使用 testing.T

我想运行一些测试用例,需要启动GRPC模拟服务器。为此,我正在使用gomock库。要启动服务器,我必须将类型为testing.T的变量传递给此函数-gomock.NewController().由于这是对所有测试用例的一种初始化,我想在TestMain中执行此操作。但是TestMain只能访问testing.M那么我该如何处理这种情况呢?在TestMain中创建一个新的testing.T结构?会成功吗? 最佳答案 听起来您正在寻找BeforeEach模式。您无权访问TestMain中的testing.T对象,因为这是在测试套件运行前

go - map 范围循环中的空接口(interface)

下面的代码没有按预期工作。packagemainimport"fmt"funcmain(){questions:=make(map[int]interface{})questions[1]=map[interface{}]string{"q1":"ThisisQuestion-1?","op1":"ThisisOption-1","op2":"ThisisOption-2",true:"ThisisOption-1",}//Thisgivemap[interface{}]stringfmt.Printf("%T\n",questions[1])//Thisnotworkingforke

go - 我如何创建一个通用函数来在 go lang 中接收 map ?

如何将map数据传递给通用函数(isExist)以检查给定值是否存在传递的map类型可以是map[int]int或map[string]string或任何funcIsExist(textint,datamap[interface{}]interface{})bool{forkey,_:=rangedata{ifdata[key]==text{returntrue}}returnfalse}funcmain(){vardata=make(map[string]int)//vardata=map[interface{}]interface{}thiscasewillworkingfined

golang 默认将 json 对象解码为 map[string]interface{},如何将其解码为 []byte?

默认情况下,golang将json对象解码为map[string]interface{},如何将其解码为[]byte?因为我需要在获得其类型后将其二次解码为结构实例。 最佳答案 为什么不直接将json解码到结构中?或者如果你有更多的对象到结构的slice中?packagemainimport("encoding/json""fmt")typeTestJsonstruct{FoostringBazstring}var(jsonValue=`{"FOO":"BAR","BAZ":"QUX"}`jsonValueSlice=`[{"FOO

json - golang 无法反射(reflect)到 map[interface{}]interface{}

我原来的问题是我想解析URL.Values到通用类型(map[interface{}]interface{})编辑/添加一些值,然后将其转换为JSON字符串并将其放入PostgreSQLJSON列。我尝试用这段代码来解析它,但是content似乎是null而err是false。request.URL.Query()打印一个漂亮的map对象。v:=reflect.ValueOf(request.URL.Query())i:=v.Interface()content,err:=i.(map[interface{}]interface{})//DosomeoperationsjsonStri

Go map 有重复的键

我正在开发一个通过UDP接收ascii消息的小型Go程序。我想查找消息中的第一个字段,看看它是否存在于map中。Go认为映射中不存在该键,但它确实存在。我可以将key添加到map并创建一个新条目,因此我有两个具有相同key的条目。我做错了什么还是这是一个错误?编辑:我简化了测试以删除UDP和YAML。https://play.golang.org/p/2Bg8UjhfWCpackagemainimport("fmt""strings")typeTestCasestruct{TeststringResultstring}funcmain(){tcmap:=make(map[string]

json - 将带有 Json 数据的字符串传递给 Golang 中的 Map

这个问题在这里已经有了答案:GolangparseJSONarrayintodatastructure(3个答案)关闭5年前。目前我已将json对象作为字符串存储在我的数据库中。我想将它们传递给map,以便能够查询任何字段:Mymap["Name"]Mymap["Age"]..假设我的字符串是这样的:'{"Name":["zero"],"Age":"10"}'我不知道数据的结构,所以Json可以有很多字段,也可以嵌套很多层(但我更担心至少得到第一层)

testing - 为什么 GoConvey 测试失败并显示错误代码 0?

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭4年前。Improvethisquestion我需要在构建过程中运行GoConvey测试我如何确保gotest以错误退出代码(非0)退出?