@2iq/aws-sdk-nock-responses 中文文档教程
aws-sdk-nock-responses
aws-sdk 的 nock 响应集合。
Quickstart
将 @2iq/aws-sdk-nock-responses
添加到 devDependencies:
{
// ...
"devDependencies": {
// ...
"@2iq/aws-sdk-nock-responses": "0.0.1",
// ...
}
// ...
}
使用库(ava 示例):
const awsApiMock = require('@2iq/aws-sdk-nock-responses'); // 1️⃣
const underTest = require('..'); // presumed we are in `test` folder
test('should create ecr repo if not existent', t => {
awsApiMock.ecr.repoDoesNotExist(); // 2️⃣
awsApiMock.ecr.repoCreationSuccessful(); // 2️⃣
awsApiMock.ecr.lifecycleUpdateSuccessfull(); // 2️⃣
underTest.run(); // 3️⃣
t.true(awsApiMock.isDone()); // 4️⃣
});
1️⃣ require
模块
2️⃣ 存根 AWS API 响应
3️⃣运行你要测试的代码(调用AWS API的代码)
4️⃣ 断言对 AWS API 的所有调用都已调用
Disclaimer
我们创建此模块是为了避免在测试文件和项目之间复制和粘贴箭尾对象。 截至目前,它仅包含我们项目所需的响应。 如果我们在测试中不需要,则不会实现响应的自定义(如设置自己的区域或帐户 ID)。 在这种情况下,这些值是硬编码的。
欢迎投稿!
aws-sdk-nock-responses
Collection of nock responses for aws-sdk.
Quickstart
Add the @2iq/aws-sdk-nock-responses
to devDependencies:
{
// ...
"devDependencies": {
// ...
"@2iq/aws-sdk-nock-responses": "0.0.1",
// ...
}
// ...
}
Use the lib (ava example):
const awsApiMock = require('@2iq/aws-sdk-nock-responses'); // 1️⃣
const underTest = require('..'); // presumed we are in `test` folder
test('should create ecr repo if not existent', t => {
awsApiMock.ecr.repoDoesNotExist(); // 2️⃣
awsApiMock.ecr.repoCreationSuccessful(); // 2️⃣
awsApiMock.ecr.lifecycleUpdateSuccessfull(); // 2️⃣
underTest.run(); // 3️⃣
t.true(awsApiMock.isDone()); // 4️⃣
});
1️⃣ require
the module
2️⃣ Stub AWS API responses
3️⃣ Run the code you want to test (code that calls the AWS API)
4️⃣ Assert that all calls to AWS API were called
Disclaimer
We created this module to avoid copy and paste of nock objects between test-files and projects. As of now, it only contains responses that are required in our projects. The customization of response (like setting own region or accountId) is not implemented if we don't need that in our test. In that case, the values are hardcoded.
Contributions are welcome!