Bil*_*ell 6 unit-testing amazon-ec2 aws-cdk
我正在编写一个创建 ECS Fargate 堆栈的 AWS CDK 应用程序。它使用现有的 VPC 和现有的 ECR 存储库。简单地模拟我的接口,并返回 IVpc 和 IRepository 接口的模拟可以帮助我解决最初的问题,但是当 CDK 使用这些构造时,我会遇到更多错误。
const mockResources = mock<IExistingResources>(
{
getVpc: (scope: cdk.Construct, vpcId: string) => {
return mock<IVpc>();
},
getElasticContainerRepository: (scope: cdk.Construct, id: string, repositoryName: string) => {
return mock<IRepository>();
}
}
);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
TypeError: Cannot destructure property 'subnetIds' of 'baseProps.vpc.selectSubnets(...)' as it is undefined.
Run Code Online (Sandbox Code Playgroud)
这似乎是一个可能的“黑洞”,我需要了解模拟的每种用法并解释它。我正在寻找一种更好的方法来对现有资源进行一致建模,以便我可以测试我的新代码。任何意见,将不胜感激。
对于遇到这个问题的任何人,我决定构建一个具有已知参数的 VPC 来进行测试。该 VPC 是从模拟fromLookup函数返回的。
例如:搭建VPC
function buildVpc(scope: cdk.Construct, vpcId:string): IVpc {
return new Vpc(scope, vpcId, {
cidr: '10.0.0.0/16',
maxAzs: 2,
subnetConfiguration: [{
cidrMask: 26,
name: 'isolatedSubnet',
subnetType: SubnetType.PUBLIC,
}],
natGateways: 0
});
}
Run Code Online (Sandbox Code Playgroud)
然后在我的类的模拟中使用 vpc ExistingResources,
const mockResources = mock<IExistingResources>(
{
getVpc: (scope: cdk.Construct, vpcId: string) => {
return buildVpc(scope, vpcId);
},
getElasticContainerRepository: (scope: cdk.Construct, id: string, repositoryName: string) => {
return mock<IRepository>();
}
}
);
Run Code Online (Sandbox Code Playgroud)
这允许我在断开连接的环境中进行快照测试。
| 归档时间: |
|
| 查看次数: |
4782 次 |
| 最近记录: |