【发布时间】: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() 链式函数再次检查数据库。终点只是data 到dataNew,在then() 函数中,它仍然返回原始data。
关于如何验证数据库中的新记录有什么想法吗?
参考资料:
-
Supertest: Verify database after request - 在底部的 TLDR 中,解决方案是使用
co。我试过了,但遇到了问题,可能是因为我使用的是await而不是生成器。
【问题讨论】:
标签: javascript node.js mocha.js amazon-dynamodb supertest