【发布时间】:2019-10-29 16:32:08
【问题描述】:
我正在尝试简化 Express 后端的测试脚手架。我不想多次调用单个记录插入例程,这在检查结果时似乎存在同步问题,我想使用数组一个接一个地执行数据库插入。
这是我到目前为止所拥有的......但我似乎在不知何故搞砸了异步等待。测试失败,因为我得到了 0 个身体长度,也许还有一个插入。有什么想法我哪里出错了吗?
async function asyncForEach (array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
};
}
async function insertTemplates(myTemplatesArray) {
const client = new Client(dbConfig);
client.connect();
await asyncForEach(
myTemplatesArray,
async (myTemplate) => {
let { name, status, version } = myTemplate;
await client.query(
SQL`INSERT INTO templates (name, status, version )
VALUES (${name}, ${status}, ${version})`)
.catch( err => console.error(err) );
});
client.end();
}
function randomTemplate() {
const template = {
name: faker.internet.userName(),
status: faker.hacker.phrase(),
version: String(faker.random.number())
};
return template;
}
function clean_db(){
const client = new Client(dbConfig);
client.connect();
client.query(
`TRUNCATE TABLE "templates";
ALTER SEQUENCE "templates_id_seq" RESTART WITH 1;`)
.catch( err => { console.error(err); } )
.then( () => client.end() );
}
describe('When there are 1 or more records in the table', () => {
const templ1 = randomTemplate();
const templ2 = randomTemplate();
const templ3 = randomTemplate();
beforeEach(async () => {
await clean_db();
await insertTemplates([templ1,templ2,templ3]);
});
afterEach(async () => {
await clean_db();
});
test('It should respond with a 200 status', async done => {
const response = await template(app).get('/templates');
expect(response.status).toBe(200);
done();
});
test('It should respond with an JSON array of records', async done => {
const response = await template(app).get('/templates');
expect(response.body.length).toBe(3);
done();
});
test('It should respond with the correct records', async done => {
templ1.id = 1;
templ2.id = 2;
templ3.id = 3;
const response = await template(app).get('/templates');
expect(response.body).toStrictEqual([templ1,templ2,templ3]);
done();
});
});
});
【问题讨论】:
-
VALUES (${name}, ${status}, ${version})似乎没有被转义,我无法想象插入工作。 -
侧节点:可以去掉
async done => {和done();,换成:async () => {。 -
VALUES (${name}, ${status}, ${version})在反引号中,我正在使用“sql-template-strings”包,因此SQL前缀/调用,我不得不说这是一个相当不错的包. -
谢谢,下次一定要注意后面勾前面的东西。
标签: arrays node.js async-await