我正在使用gomock,我有一段示例代码,我想测试一下。
type StructA struct {
client map[string]Foo
}
type Foo interface {
foo.methodFoo() string
}
func (a *structA) MethodA(name string) string {
client := a.client[name]
return client.methodFoo()
}
在我的测试中,我为 foo 生成了一个模拟,称为 mockFoo。使用模拟 v1.6.0 为接口 foo 生成模拟。
我的测试代码为:
func Test_MethodA(t *testing.T) {
type fields struct {
client map[string]*mockFoo
}
tests := []struct {
fields fields
want string
}
{
// test cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &StructA {
client: tt.fields.client //this has an error, saying that we cannot use map[string]*mockFoo as the type map[string]foo
}
...
})
}
}
TLDR 是不能用作类型 。map[string]*mockFoomap[string]Foo
var foo1 Foo
var mockFoo1 *mockFoo
foo1 = mockFoo1 // no problem
var fooMap map[string]Foo
var mockFooMap map[string]*mockFoo
fooMap = mockFooMap // problematic
我能知道我在这里是否做错了什么,或者这是否是预期的行为吗?谢谢。
回首忆惘然
相关分类