【发布时间】:2021-11-18 10:14:56
【问题描述】:
我是 golang 新手,使用 Gomock 进行测试。我已经为接口foo 生成了模拟,但是在我的代码中有一段使用foo.(type) 的逻辑。
我可以知道是否有办法模拟这个并返回我选择的类型吗?如果没有,那么做这件事的好方法是什么?谢谢!
例如。代码sn-p:
// assume structs A and B implements the interface `foo`
type foo interface {...}
type A struct {...}
type B struct {...}
func DoSomething(i foo) {
// Is there a way to mock this type assertion for i here?
switch currentType := i.(type) {
case *A:
...
case *B:
...
default:
...
}
}
【问题讨论】:
-
能分享一下实现逻辑的函数的sn-p吗?
-
没问题,让我编辑帖子!