为未明确 return 值的函数编写 Mocha 单元测试
Writing a Mocha unit test for a function that does not explicitly return a value
我有一个关于在我的 Node 应用程序中编写 Mocha 单元测试的问题,当函数没有明确 return 任何东西时。
具体来说,我要编写测试的函数迭代两个数组——一个用于插入,一个用于更新。该函数如下所示:
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);
}
}
}
};
现在我的 mocha 测试的相关部分看起来像这样:
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);
});
但在我这里的例子中,因为我没有在正在测试的函数中明确 return 任何东西,所以 result
最终是——即使函数运行成功——是undefined
。因为我的函数使用 for-of
循环,所以我不想在我的 dataSetsMergeOp()
函数中使用 return await
,因为虽然这会使测试通过,但它会在第一次迭代。
那么对于这种功能,推荐的测试方法是什么?
要单元测试函数,我希望函数不会抛出任何异常。
您可能想要 return 类似 { updatedDocumentsCount: n }
的东西,并在每次循环迭代时递增它,并断言它等于您传递的文档数。
对于集成测试,您想在函数执行前后查询您的数据库,并检查您是否实际更新/插入了预期数量的文档。
我有一个关于在我的 Node 应用程序中编写 Mocha 单元测试的问题,当函数没有明确 return 任何东西时。
具体来说,我要编写测试的函数迭代两个数组——一个用于插入,一个用于更新。该函数如下所示:
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);
}
}
}
};
现在我的 mocha 测试的相关部分看起来像这样:
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);
});
但在我这里的例子中,因为我没有在正在测试的函数中明确 return 任何东西,所以 result
最终是——即使函数运行成功——是undefined
。因为我的函数使用 for-of
循环,所以我不想在我的 dataSetsMergeOp()
函数中使用 return await
,因为虽然这会使测试通过,但它会在第一次迭代。
那么对于这种功能,推荐的测试方法是什么?
要单元测试函数,我希望函数不会抛出任何异常。
您可能想要 return 类似 { updatedDocumentsCount: n }
的东西,并在每次循环迭代时递增它,并断言它等于您传递的文档数。
对于集成测试,您想在函数执行前后查询您的数据库,并检查您是否实际更新/插入了预期数量的文档。