Unit testing 在通道中设置数据,该通道实际上由模拟函数为单元测试设置
我正在尝试为一个通过hystrix.Go进行调用的方法(methA)编写单元测试。我在Hystrix func()内调用的外部函数[somefunc]将通道作为参数,并将数据插入实际代码流中的通道中。在编写单元时,我想将数据设置到通道中,并测试所有可能性,这是可能的吗Unit testing 在通道中设置数据,该通道实际上由模拟函数为单元测试设置,unit-testing,go,goroutine,hystrix,Unit Testing,Go,Goroutine,Hystrix,我正在尝试为一个通过hystrix.Go进行调用的方法(methA)编写单元测试。我在Hystrix func()内调用的外部函数[somefunc]将通道作为参数,并将数据插入实际代码流中的通道中。在编写单元时,我想将数据设置到通道中,并测试所有可能性,这是可能的吗 func methA() { // some code channel := make(chan []string, 1) hystrix.Go("x", func() error { re
func methA() {
// some code
channel := make(chan []string, 1)
hystrix.Go("x", func() error {
return A.somefunc(channel)
}, nil)
// some code which uses channel
}
使用哪个通道的测试代码?也许你可以试着模仿
A.somefunc
例如:
func TestMethA(t testing.t){
//一切可能性
案例:=struct{
data[]接口{}//发送到通道的数据
想要接口{}
} {
// ...
}
somefunc:=A.somefunc
延迟函数(){
A.somefunc=somefunc
}()
对于c:=范围案例{
A.somefunc=func(c chan)错误{
//将数据设置到通道中。
}
//调用methodA
方法a()
}
}
通过单元测试,您无法访问通道。也许您可以重构methA()来创建一个通道并调用methB(通道),这样您就可以在测试中创建一个通道,然后改为测试methB。