首页 > 解决方案 > 如何使用 dynamodb 查询操作模拟/单元测试分页?

问题描述

我已经成功地实现了向前/向后分页以及使用 dynamoDb 查询在后端进行排序。但是我一直坚持提供分页数据或排序数据。

有没有办法在测试期间加载虚拟数据?

我的测试代码

test('should paginate backward and forward', async () => {
    const data = {
      Items: [
        {
          convertedUrl: 'https://xxxx.awssensei.tk/ddddd',
          createdAt: '2021-11-04T05:50:14.180Z',
          originalUrl: 'https://aws.com',
          updatedAt: '2021-11-04T05:58:12.659Z',
        },
        {
          convertedUrl: 'https://xxxx.awssensei.tk/bbbbb',
          createdAt: '2021-11-04T05:50:43.818Z',
          originalUrl: 'https://fb.com',
          updatedAt: '2021-11-04T05:57:42.704Z',
        },
        {
          convertedUrl: 'https://xxx.awssensei.tk/eeeee',
          createdAt: '2021-11-04T05:50:55.121Z',
          originalUrl: 'https://google.com',
          updatedAt: '2021-11-04T05:58:21.141Z',
        },
        {
          convertedUrl: 'https://xxxx.awssensei.tk/aaaaa',
          createdAt: '2021-11-04T05:51:06.603Z',
          originalUrl: 'https://instagram.com',
          updatedAt: '2021-11-04T05:57:14.750Z',
        },
      ],
    };

    sandbox
      .stub(AWS.DynamoDB.DocumentClient.prototype, 'query')
      .returns({ promise: () => data });

    const app = require('../app');
    const response = await request(app)
      .get('/users/auth-user/urls?sort_by=createdAt')
      .set('Authorization', `Bearer ${token}`);
    expect(response.body.history.length).toBe(4);
  });

标签: node.jsamazon-web-servicesjestjsamazon-dynamodbsinon

解决方案


推荐阅读