开玩笑地验证DynamoDB方法的测试用例

时间:2020-09-01 04:09:04

标签: javascript amazon-web-services unit-testing jestjs amazon-dynamodb

我正在编写Jest测试用例以验证DynamoDB方法。对于所有DynamoDB方法,我都有一个包装器类,其中包含使用DocumentClient类调用的所有方法。

export class DynamoDBRepository<T, S> implements IDynamoDBWrite<T, S>, IDynamoDBRead<T, S> {
  constructor(
    protected readonly docClient: DocumentClient,
    protected readonly tableName: string,
    private readonly entityClass: EntityConstructor<T>,
  ) {
  }

  public async create(item: Partial<T>): Promise<T> {
    const putParams: DocumentClient.PutItemInput = {
      TableName: "TABLE_NAME",
      Item: item,
    };
    await this.docClient.put(putParams).promise();
    return new this.entityClass(putParams.Item);
  }

  public async update(key: S, item: Omit<Partial<T>, keyof S>): Promise<T> {
    const updateParams: DocumentClient.PutItemInput = {
      TableName: "TABLE_NAME",
      Item: {
        ...item,
        ...key,
      },
    };
    await this.docClient.put(updateParams).promise();
    return new this.entityClass(updateParams.Item);
  }
}

像上面一样,有两个创建和更新方法,它们内部都在调用DynamoDB方法(this.docClient)。现在,另一个扩展了DynamoDBRepository类,以便它可以使用其所有方法:

import { DocumentClient } from 'aws-sdk/clients/dynamodb';
export class JobScheduleRepository extends DynamoDBRepository<JobSchedule, JobScheduleKey> {
  public static TABLE_NAME = process.env.RUNTIME_JOB_SCHEDULE_REPOSITORY_NAME;

  constructor(docClient: DocumentClient) {
    super(docClient, JobScheduleRepository.TABLE_NAME, JobSchedule);
  }
}

现在使用Jest编写测试用例时,我正在创建JobScheduleRepository类的实例,然后调用DynamoDBRepository类方法来测试功能。我直接调用诸如create和update之类的方法,然后模拟DocumentClient,以便我可以操纵实际DynamoDB调用(this.docClient)的结果,并可以测试DynamoDBRepository类方法,如create,update等。

import { DocumentClient, ClientConfiguration } from 'aws-sdk/clients/dynamodb';
import { JobScheduleRepository } from '../../../../functions/scheduler/src/job-schedule.repository';
import { JobSchedule } from '../../../../functions/scheduler/src/job-schedule.entity';


describe('DynamoDB test coverage', () => {
    const JobScheduleRepositoryInstance = new JobScheduleRepository(new DocumentClient());
    /*For below simple test cases it works fine*/
    describe('Instances and method type validations', () => {
        it('Check instance of class', () => {
            expect(JobScheduleRepositoryInstance).toBeInstanceOf(JobScheduleRepository);
        });
        it('Method type validations', () => {
            expect(JobScheduleRepositoryInstance.find).toBeInstanceOf(Function);
            expect(JobScheduleRepositoryInstance.findAll).toBeInstanceOf(Function);
            expect(JobScheduleRepositoryInstance.batchWrite).toBeInstanceOf(Function);
            expect(JobScheduleRepositoryInstance.create).toBeInstanceOf(Function);
            expect(JobScheduleRepositoryInstance.delete).toBeInstanceOf(Function);
            expect(JobScheduleRepositoryInstance.findMany).toBeInstanceOf(Function);
            expect(JobScheduleRepositoryInstance.findOne).toBeInstanceOf(Function);
            expect(JobScheduleRepositoryInstance.update).toBeInstanceOf(Function);
        });
        it('TABLE_NAME exists on class', () => {
            expect(JobScheduleRepository.TABLE_NAME).toEqual(process.env.RUNTIME_JOB_SCHEDULE_REPOSITORY_NAME);
        });
    });

    describe('DynamoDB methods test coverage', () => {
        const documentClient = new DocumentClient();
        /*Facing issues in these types of test cases, where I need to mock dynamoDB Client*/
        it('Create call is successful', async () => {
            const params = {
                TableName: 'TABLE_NAME',
                Item: {
                    pk: 'T1',
                    sk: 'SK1',
                    status: 0
                }
            };
            documentClient.put(params).promise = jest.fn().mockImplementation(() => Promise.resolve('Hello'));
            try {
                const createResponse = await JobScheduleRepositoryInstance.create(params);
                const putResponse = await documentClient.put(params).promise();
                expect(putResponse).resolves.toBe('Hello');
                expect(createResponse).toBeInstanceOf(JobSchedule);
            } catch(e) {
                throw new Error(e);
            }
        });
    });
});

运行上面的测试时,我得到下面的输出。

enter image description here

测试用例因“创建呼叫成功”而失败。我的问题是,我是否在模拟documentClient.put方面以正确的方式进行操作,还是说我是否还在模拟文件client.update?由于我是开玩笑的新手,请问您如何建议对此进行测试?

还要注意,我在DynamoDBRepository类方法中对dynamoDB方法的调用就像 this.docClient 使用了此功能,因此直到将其更改为以下代码之前,我的模拟函数才被调用:

documentClient.put(params).promise = jest.fn().mockImplementation(() => Promise.resolve('Hello'));

这是正确的方法吗?

0 个答案:

没有答案
相关问题