草庐IT

cost_per_unit

全部标签

unit-testing - 来自 golang 包的模拟方法

我一直无法找到从golang包中模拟方法的解决方案。例如,我的项目有代码在Os.Getwd()返回错误时尝试恢复。我能想到的对此进行单元测试的最简单方法是模拟Os.Getwd()方法以返回错误,并验证代码是否相应地工作。我尝试使用testify,但它似乎不可行。谁有这方面的经验? 最佳答案 我自己的解决方案是将方法作为参数,这允许在测试时注入(inject)“模拟”。此外,创建一个导出方法作为公共(public)外观和一个未导出的方法用于测试。例子:funcFoo()int{returnfoo(os.Getpid)}funcfoo(

unit-testing - 带有 `time.Time` 的 Go 类型开关

在我的测试中,我有一个函数可以从这样的结构中获取值:funcgetField(vinterface{},fieldstring)string{r:=reflect.ValueOf(v)f:=reflect.Indirect(r).FieldByName(field)t:=f.Kind()switcht{casereflect.Int,reflect.Int64:returnstrconv.FormatInt(f.Int(),10)casereflect.String:returnf.String()casereflect.Bool:iff.Bool(){return"true"}ret

unit-testing - 带有 `time.Time` 的 Go 类型开关

在我的测试中,我有一个函数可以从这样的结构中获取值:funcgetField(vinterface{},fieldstring)string{r:=reflect.ValueOf(v)f:=reflect.Indirect(r).FieldByName(field)t:=f.Kind()switcht{casereflect.Int,reflect.Int64:returnstrconv.FormatInt(f.Int(),10)casereflect.String:returnf.String()casereflect.Bool:iff.Bool(){return"true"}ret

unit-testing - 我可以期望(要求)go 测试失败吗?

在我使用的其他测试框架中,当编写测试助手时,能够自动测试它们是很好的,即测试它们是否通过以及是否失败。让我使用以下帮助程序(实际上要复杂得多):funcIsRedirect(t*testing.T,codeint){assert.True(t,code>=300)assert.True(t,code当然,我可以写:funcTestIsRedirect(t*testing.T){IsRedirect(t,http.StatusSeeOther)}但我也想写这样的东西:funcTestNotRedirect(t*testing.T){t.RequireFailure()IsRedirect

unit-testing - 我可以期望(要求)go 测试失败吗?

在我使用的其他测试框架中,当编写测试助手时,能够自动测试它们是很好的,即测试它们是否通过以及是否失败。让我使用以下帮助程序(实际上要复杂得多):funcIsRedirect(t*testing.T,codeint){assert.True(t,code>=300)assert.True(t,code当然,我可以写:funcTestIsRedirect(t*testing.T){IsRedirect(t,http.StatusSeeOther)}但我也想写这样的东西:funcTestNotRedirect(t*testing.T){t.RequireFailure()IsRedirect

unit-testing - 如何正确测试调用其中另一个函数的处理程序

我正在测试看起来像这样的PostUser函数(为简单起见省略了错误处理):funcPostUser(env*Env,whttp.ResponseWriter,req*http.Request)error{decoder:=json.NewDecoder(req.Body)decoder.Decode(&user)iflen(user.Username)30{returnStatusError{400,errors.New("usernamesneedtobemorethan2charactersandlessthan30characters")}}emailRe:=regexp.Mus

unit-testing - 如何正确测试调用其中另一个函数的处理程序

我正在测试看起来像这样的PostUser函数(为简单起见省略了错误处理):funcPostUser(env*Env,whttp.ResponseWriter,req*http.Request)error{decoder:=json.NewDecoder(req.Body)decoder.Decode(&user)iflen(user.Username)30{returnStatusError{400,errors.New("usernamesneedtobemorethan2charactersandlessthan30characters")}}emailRe:=regexp.Mus

unit-testing - Golang 模拟函数被调用两次

我正在编写用于测试main.go的单元测试,并在函数内部调用Get函数(DeviceRepo.Get())两次,然后我想模拟返回不同的Get函数,但我可以在第一次模拟时模拟它调用了,所以我不知道如何在第二次模拟Get函数?main.go:typeDeviceInterfaceinterface{}typeDeviceStructstruct{}varDeviceReporepositories.DeviceRepoInterface=&repositories.DeviceRepoStruct{}func(d*DeviceStruct)CheckDevice(familynamestr

unit-testing - Golang 模拟函数被调用两次

我正在编写用于测试main.go的单元测试,并在函数内部调用Get函数(DeviceRepo.Get())两次,然后我想模拟返回不同的Get函数,但我可以在第一次模拟时模拟它调用了,所以我不知道如何在第二次模拟Get函数?main.go:typeDeviceInterfaceinterface{}typeDeviceStructstruct{}varDeviceReporepositories.DeviceRepoInterface=&repositories.DeviceRepoStruct{}func(d*DeviceStruct)CheckDevice(familynamestr

unit-testing - 如何测试创建和注入(inject)依赖项的函数

我的问题是您如何决定在何处注入(inject)依赖项,以及如何测试首次将依赖项注入(inject)函数的函数?例如,我正在重构一些Go代码以使用依赖注入(inject),目的是让代码更易于测试。这是我重构后的代码的样子:typeFooIfaceinterface{FooFunc()}typeFoostruct{}func(f*Foo)FooFunc(){//SomefunctionIwouldliketostub}funcmain(){OuterFunction()}funcOuterFunction(){fooVar:=&Foo{}InnerFunction(fooVar)//Oth