【问题标题】:supertest and checking DynamoDB before and after using await在使用 await 之前和之后进行超级测试和检查 DynamoDB
【发布时间】:2017-09-25 13:14:01
【问题描述】:

我想使用 supertest 测试我的 koa API 路由并检查 DynamoDB 之前和之后的内容,以确保端点符合预期。

// app related
const pool = require('../../src/common/pool');
const app = require('../../server');
// for testing
const uuid = require('uuid');
const supertest = require('supertest');
// listen on port 40002
const request = supertest.agent(app.listen(4002));
describe('test', () => {
  it.only('should', async (done) => {
    debugger;
    const id = uuid.v4().replace(/-/g, '');
    await pool.add(id, 'data', 30);
    return request
      .get('/api/1')
      .expect(204)
      // .then(async (res) => {
      // .then((res) => {
      .end((res) => {
        // still returns 'data' instead of 'dataNew' after the route is hit
        const record = await pool.get(id);
        debugger;
        done();
      });
  });
});

在上面的代码中,我在数据库中创建了一条记录,然后我到达了终点,我尝试了then()end() 链式函数再次检查数据库。终点只是datadataNew,在then() 函数中,它仍然返回原始data

关于如何验证数据库中的新记录有什么想法吗?

参考资料:

【问题讨论】:

    标签: javascript node.js mocha.js amazon-dynamodb supertest


    【解决方案1】:

    上述问题已通过将返回承诺的pool.add() 链接到超测试request 然后await 记录以验证它来修复。有时它仍然太快获得记录,因为pool.update() 方法不是awaited 在request 命中的端点内。

    describe('test', () => {
      it.only('should', async () => {
        const id = uuid.v4().replace(/-/g, '');
        await pool.add(id, 'data', 30).then(() => {
          return request
            .get('/api/1')
            .expect(204)
            // check custom headers
            //.expect('pool-before', 'data')
            //.expect('pool-after', 'dataModified')
            .then(async (res) => {
              const record = await pool.get(id);
              debugger;
              expect('dataModified').to.equal(record.fields.S);
            });
        });
      });
    });
    

    我能想到的唯一其他方法是通过自定义标头、延迟或使用模拟来检查值。

    如果有人有更好的解决方案,请告诉我。

    【讨论】:

      猜你喜欢
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      相关资源
      最近更新 更多