草庐IT

bool_test

全部标签

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

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

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

unit-testing - 无法在 GoLand IDE 内的测试套件中运行单个测试?

我正在使用testify'stestsuite支持编写单元测试。这导致我的测试文件有一个TestFooBar(t*testing.T)启动suite.Run而我所有的单独测试都成为我的测试套件的一部分struct方法签名如-func(suite*myTestSuite)TestMyStuff()。我观察到GoLand可以识别所有带有类似于TestFooBar(t*testing.T)签名的方法,并在其旁边放置一个绿色的播放图标。它将允许我单独运行/调试这些方法。但是,作为上述测试套件一部分的所有测试方法都不会被识别,也无法在IDE中单独运行或调试。有什么方法可以告诉GoLandmyTe

mongodb - 无法解析行 #126 : mongo. 数据库 = mongo_db_test

我正在学习本教程Golang+Revelwebframework+MongodbRESTFulgeneratorfor(revel_mgo)一步一步来,但是当我最终结束它并尝试运行它时,它抛出了这个错误CRIT16:11:18revel_container.go:139:无法加载配置文件error="C:\Users\Userx\go\src\RevelApp\conf\app.conf:无法解析第126行:mongo.database=RevelApp"第126行是这样的:[dev]126-mongo.database=mongo_db_test127-mongo.path=127.

go - chan bool 是如何让 goroutine 等待的?

我正在构建一个应用程序,以便在每次代码更改时运行命令。我为此功能使用了fsnotify。但是,我不明白它是如何等待主协程的。我发现使用sync.WaitGroup更为惯用,但我很好奇chanbool如何让goroutine在fsnotify示例代码中等待。我试图在fsnotify的示例代码中删除done,但它没有等待goroutine,只是退出了。watcher,err:=fsnotify.NewWatcher()iferr!=nil{log.Fatal(err)}deferwatcher.Close()done:=make(chanbool)gofunc(){for{select{c

unit-testing - 如何使用 os.O_RDWR 和 os.O_CREATE 标志测试 file.Open?

我正在尝试为结构构造函数编写单元测试,如果在file.Open期间发生错误,它也可能返回nil。我不知道如何使用标志测试/模拟文件错误:os.O_RDWR|os.O_CREATE|os.O_APPEND我试图在测试中检查nil值,但失败了。构造函数:typeAppstruct{someFieldstringlog*log.Logger}funcNew()*App{app:=&App{}f,err:=os.OpenFile("info.log",os.O_RDWR|os.O_CREATE|os.O_APPEND,0666)iferr!=nil{fmt.Printf("erroropeni

go - Go 中的 bool 函数

请帮忙,我是Go的新手。我编写了函数,将字符串传递给正则表达式并返回bool值。在验证正确的出生日期格式时,我的测试一直失败。我的测试:funcTestIsMatchingRegex(t*testing.T){t.Parallel()vartests=[]struct{dobstringregstringexpectedbooldescstring}{{dob:"1928-06-05",reg:`[12][0-9]{3}-[01][0-9]-[0-3][0-9]`,expected:true,desc:"test1"},{dob:"1928/06/05",reg:`[12][0-9]{

testing - 如何在 Golang 中测试日志记录(log.Println)?

如何在Golang中测试下面的代码?packagemainimport"log"funcmain(){log.Println("helloworld")} 最佳答案 您的程序写入log包标准记录器,后者写入标准错误。编译并运行你的程序,看看标准错误中出现了什么。 关于testing-如何在Golang中测试日志记录(log.Println)?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/quest

unit-testing - 如何对包装对外部服务的 http 调用的处理程序进行单元测试

我有一个名为CreateObject的处理函数。此函数同时包装了对我无法控制的外部API的POST请求。如果我想对其进行单元测试,我面临的问题是我无法在每次运行测试时都将新对象发布到外部服务。所以我想知道是否有办法用Go或任何解决方法来模拟它。非常感谢。主包funcmain(){router:=mux.NewRouter()router.HandleFunc("/groups",services.CreateObject).Methods("POST")c:=cors.New(cors.Options{AllowedOrigins:[]string{"*"},AllowCredenti