2017-09-25 63 views
0

我想用supertest来测试我的koa API路线和检查什么在DynamoDB之前和之后,以确保端点做了什么之意。supertest前和使用后的await检查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()链接功能再次检查分贝。终点将只是datadataNewthen()函数,它仍会返回原始的data

关于如何验证db中新记录的任何想法?

参考文献:

  • ​​3210 - 在TLDR底部,该解决方案是使用co。我试过这个,并有问题可能导致我使用await而不是发电机。

回答

0

上述固定通过链接它返回一个承诺的pool.add()到supertest request然后await荷兰国际集团,以验证它的记录。有时它仍然得到记录的速度太快,因为正在击中的终点内的方法不是await

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); 
     }); 
    }); 
    }); 
}); 

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

让我知道是否有人有更好的解决方案。