我在创建 IAM 角色的 Typescript 中创建了一个 AWS CDK 应用程序。这是我的代码:
export class AccountCreatorIamRoleStack extends cdk.Stack {
public create(): iam.Role {
const iamRole = some logic to create iam role goes here;
return iamRole;
}
}
我已经通过创建一个像这样的 IAM 角色确认此代码有效:
var roleCreator = new AccountCreatorIamRoleStack(app, 'AccountCreatorIamRoleStack');
roleCreator.create();
但是,当我运行这个 Jest 单元测试时:
test('Test IAM Role Created', () => {
const app = new cdk.App();
// WHEN
var stack = new AccountCreatorIamRoleStack(app, 'TestAccountCreatorIamRoleStack').create()
// THEN
expectCDK(stack).to(haveResource("AWS::IAM::Role"));
});
失败并出现以下错误:
FAIL test/account-creation-iam-role-stack.test.ts
✕ Test IAM Role Created (32 ms)
● Test IAM Role Created
None of 0 resources matches resource 'AWS::IAM::Role' with {
"$anything": true
}.
16 | var stack = new AccountCreatorIamRoleStack(app, 'TestAccountCreatorIamRoleStack').create()
17 | // THEN
> 18 | expectCDK(stack).to(haveResource("AWS::IAM::Role"));
| ^
19 | });
at HaveResourceAssertion.assertOrThrow (node_modules/@aws-cdk/assert/lib/assertions/have-resource.ts:100:13)
at StackInspector._to (node_modules/@aws-cdk/assert/lib/inspector.ts:25:15)
at StackInspector.to (node_modules/@aws-cdk/assert/lib/inspector.ts:15:14)
at Object.<anonymous> (test/account-creation-iam-role-stack.test.ts:18:20)
我可以看到它指向那条线上的东西,但它究竟指向什么它认为是错误的?想法?
最佳答案
您的代码断言 AWS::IAM::Role
具有 "VisibilityTimeout": 300
但它没有这样的属性。当您的代码实际创建它时,它应该单独传递给 haveResource("AWS::IAM::Role")
(没有其他属性)。
以下是 IAM 角色的当前属性:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html
{
"Type" : "AWS::IAM::Role",
"Properties" : {
"AssumeRolePolicyDocument" : Json,
"Description" : String,
"ManagedPolicyArns" : [ String, ... ],
"MaxSessionDuration" : Integer,
"Path" : String,
"PermissionsBoundary" : String,
"Policies" : [ Policy, ... ],
"RoleName" : String,
"Tags" : [ Tag, ... ]
}
}```
关于amazon-web-services - AWS CDK 单元测试失败 - jest ts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65878646/