Node.js 为未显式返回值的函数编写Mocha单元测试
我有一个问题,当函数没有显式返回任何内容时,在我的节点应用程序中编写Mocha单元测试 具体地说,我想为两个数组编写一个迭代测试——一个用于插入,另一个用于更新。该函数如下所示:Node.js 为未显式返回值的函数编写Mocha单元测试,node.js,mocha.js,Node.js,Mocha.js,我有一个问题,当函数没有显式返回任何内容时,在我的节点应用程序中编写Mocha单元测试 具体地说,我想为两个数组编写一个迭代测试——一个用于插入,另一个用于更新。该函数如下所示: module.exports = async function dataSetsMergeOp(args) { const recordsToInsert = args.newRecords; const recordsToUpdate = args.matchingRecords; // Handle
module.exports = async function dataSetsMergeOp(args) {
const recordsToInsert = args.newRecords;
const recordsToUpdate = args.matchingRecords;
// Handle inserts
if (recordsToInsert.length > 0) {
for (let record of recordsToInsert) {
try {
const insertQuery = `
INSERT INTO cd.customer_accounts (
hypindx,
hypnumbr_1,
hypnumbr_2,
) VALUES (?, ?, ?);
`;
const args = [
record.HYPINDX,
trimmedStringOrNull(record.HYPNUMBR_1),
trimmedStringOrNull(record.HYPNUMBR_2),
];
console.log('Record inserting...');
await queryHandler(insertQuery, args);
} catch (error) {
console.log(error);
}
}
}
// Handle updates
if (recordsToUpdate.length > 0) {
for (let record of recordsToUpdate) {
try {
const updateQuery = `
UPDATE cd.customer_accounts
SET
hypindx = ?,
hypnumbr_1 = ?,
hypnumbr_2 = ?
WHERE hypindx = ?
`;
const args = [
record.ACTINDX,
trimmedStringOrNull(record.HYPNUMBR_1),
trimmedStringOrNull(record.HYPNUMBR_2),
record.HYPINDX
];
console.log('Record updating...');
await queryHandler(updateQuery, args);
} catch (error) {
console.log(error);
}
}
}
};
before(async function () {
try {
result = await dataSetsMergeOp(args);
} catch (e) {
console.log(e.stack);
}
});
it("should be truthy", function () {
assert.isOk(result);
});
it("should return an object for the 'job'", function () {
assert.isObject(result);
});
it("should return a number for the 'affectedRows' property", function () {
assert.typeOf(result.affectedRows, "number");
});
it("should return a number for the 'warningStatus' property", function () {
assert.typeOf(result.warningStatus, "number");
});
it("expect 'warningStatus' to be 0", function () {
expect(result.warningStatus).to.equal(0);
});
现在,我的摩卡咖啡测试的相关部分如下所示:
module.exports = async function dataSetsMergeOp(args) {
const recordsToInsert = args.newRecords;
const recordsToUpdate = args.matchingRecords;
// Handle inserts
if (recordsToInsert.length > 0) {
for (let record of recordsToInsert) {
try {
const insertQuery = `
INSERT INTO cd.customer_accounts (
hypindx,
hypnumbr_1,
hypnumbr_2,
) VALUES (?, ?, ?);
`;
const args = [
record.HYPINDX,
trimmedStringOrNull(record.HYPNUMBR_1),
trimmedStringOrNull(record.HYPNUMBR_2),
];
console.log('Record inserting...');
await queryHandler(insertQuery, args);
} catch (error) {
console.log(error);
}
}
}
// Handle updates
if (recordsToUpdate.length > 0) {
for (let record of recordsToUpdate) {
try {
const updateQuery = `
UPDATE cd.customer_accounts
SET
hypindx = ?,
hypnumbr_1 = ?,
hypnumbr_2 = ?
WHERE hypindx = ?
`;
const args = [
record.ACTINDX,
trimmedStringOrNull(record.HYPNUMBR_1),
trimmedStringOrNull(record.HYPNUMBR_2),
record.HYPINDX
];
console.log('Record updating...');
await queryHandler(updateQuery, args);
} catch (error) {
console.log(error);
}
}
}
};
before(async function () {
try {
result = await dataSetsMergeOp(args);
} catch (e) {
console.log(e.stack);
}
});
it("should be truthy", function () {
assert.isOk(result);
});
it("should return an object for the 'job'", function () {
assert.isObject(result);
});
it("should return a number for the 'affectedRows' property", function () {
assert.typeOf(result.affectedRows, "number");
});
it("should return a number for the 'warningStatus' property", function () {
assert.typeOf(result.warningStatus, "number");
});
it("expect 'warningStatus' to be 0", function () {
expect(result.warningStatus).to.equal(0);
});
但在我这里的例子中,因为我没有显式返回正在测试的函数中的任何内容,result
最终得到的是未定义的内容,即使函数成功运行。因为我的函数使用了一个for of
循环,所以我不想在我的dataSetsMergeOp()
函数中使用return wait
,因为虽然这会使测试通过,但它会在第一次迭代后停止循环
那么,为这类函数编写测试的推荐方法是什么呢?要对函数进行单元测试,例如,我希望函数不会抛出异常
您可能希望返回类似于{updatedDocumentsCount:n}
的内容,并在每次循环迭代时递增它,并断言它等于您传递的文档数
对于集成测试,您需要在函数执行前后查询数据库,并检查是否实际更新/插入了预期数量的文档