如何正确模拟dynamoDB调用?

时间:2019-05-26 16:07:54

标签: node.js amazon-web-services amazon-dynamodb mocha aws-sdk

我正在尝试为使用AWS开发工具包调用DynamoDB的nodeJS应用程序创建单元测试。在下面的测试中,我尝试将putItem调用模拟到DynamoDB中,但是在运行测试时,它将尝试向AWS调用。

    import AWSMock from 'aws-sdk-mock'

    ...

    it('Can create an item', mochaAsync(async () => {
        AWSMock.mock('DynamoDB', 'putItem', (params: Certification, callback: any) => {
            console.log('Returning certification content from mock');
            let temp: Certification = Object.create(certification);
            temp.id = uuid();
            return callback(null, temp);
        });

        let res: Certification = await service.upsert(certification);
        expect(res.id).empty.false;
        expect(res.name).to.eql(certification.name);
        expect(res.description).to.not.eql(certification.description);
        expect(res.dateIssued).to.eql(certification.dateIssued);
        expect(res.dateExpires).to.eql(certification.dateExpires);
        expect(res.image).to.eql(certification.image);
    }));

正在测试的代码,有关完整上下文,请参见https://github.com/bkimbrou/resume-backend

    public upsert(event: Certification, context: any = {}) : Promise<Certification> {
        return new Promise<Certification>(() => this.putItem(event));
    }

...

    protected putItem(item: T) {
        if (!item.id) {
            item.id = uuid()
        }
        let params: AWS.DynamoDB.PutItemInput = {
            Item: AbstractDynamoService.jsonToItem(item),
            TableName: this.tableName
        };

        this.dynamo.putItem(params, AbstractDynamoService.handleResult);
    }

任何对我犯错地方的见解将不胜感激!

0 个答案:

没有答案