1 回答
TA贡献1815条经验 获得超12个赞
无法模拟类型,您只能模拟接口的实现。在您的情况下,我假设您正在尝试调用DescribeVolumes并在响应中获取由您构造的值。
为此,您需要创建一个模拟
type MockEC2API struct {
ec2iface.EC2API // embedding of the interface is needed to skip implementation of all methods
DescribeVolumesMethod func(*ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error)
}
func (m *MockEC2API) DescribeVolumes(in *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {
if m.DescribeVolumesMethod != nil {
return m.DescribeVolumesMethod(in)
}
return nil, nil // return any value you think is good for you
}
在测试中创建实例MockEC2API而不是真实实例ec2.EC2并为其提供将被调用的函数并返回您准备好的ec2.DescribeVolumesOutput结果
...
ec2 := &MockEC2API{
DescribeVolumesMethod: func(*ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {
return &ec2.DescribeVolumesOutput{...your initialization...}, nil
}
}
...
output, err := ec2.DescribeVolumes(in) // this output will be your prepared initialization
- 1 回答
- 0 关注
- 110 浏览
添加回答
举报