【发布时间】:2020-08-13 06:59:24
【问题描述】:
我是在 Typescript 中模拟的初学者。我想在我的单元测试中模拟AWS.Comprehend。我在 AWS Service Comprehend 中有此代码。
const comprehend = new AWS.Comprehend();
export const handler = async (): Promise<any> => {
const params = {
JobName: "first-job",
InputDataConfig: {
S3Uri: "input_bucket_name",
InputFormat: "ONE_DOC_PER_FILE"
},
OutputDataConfig: {
S3Uri: "output_bucket_name"
},
DataAccessRoleArn: "role_arn"
};
const result = await comprehend.startDominantLanguageDetectionJob(params)
.promise()
.catch(error => {
throw error;
}
);
return result.JobId;
};
我尝试为我的代码编写单元测试。
import { expect } from 'chai';
import * as AWSMock from 'aws-sdk-mock';
import * as AWS from 'aws-sdk';
describe('unitTest', () => {
before(() => {
AWSMock.setSDKInstance(AWS);
AWSMock.mock('Comprehend', 'startDominantLanguageDetectionJob', (params, cb) => {
cb(null, { jobId: 'test_job_id', JobStatus: 'SUBMITTED' });
});
});
it('should pass', async () => {
const result = await handler();
expect(result).to.be.eql('test_job_id');
});
});
但我的代码不起作用。在我看来,Comprehend 不是一个模拟。并且运行正常的 startDominantLanguageDetectionJob 不是模拟。
使用aws-sdk-mock 有什么错误?
【问题讨论】:
标签: typescript unit-testing aws-sdk-js sinon-chai aws-sdk-mock