我刚接触golang,并且使用Gomock进行测试。我已经为接口foo生成了模拟,但在我的代码中有一段使用foo.(type)的逻辑。
我可以知道是否有一种方法可以模拟它并返回我选择的类型?如果不是,那么做这件事的好方法是什么?谢谢!
例如:代码片段:
// 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:
...
}
}发布于 2021-10-04 01:28:20
gomock为接口生成新的实现。(对于接口Foo实现是MockFoo结构)。在您的场景中,这样的模拟将只涵盖default的情况。
处理DoSomething(i foo)最简单的方法是传递具体的实现(根据您的场景测试数据)
https://stackoverflow.com/questions/69326684
复制相似问题