我有以下承诺。我知道它们工作正常,因为 funcThree 中的 console.log 输出返回了正确的数据。我如何通过测试证明这一点?
基本上,我如何测试这个承诺?我已经尝试在下面进行测试,但无论我在那里放什么(包括expect(false).to.be.true),它总是返回true。我不相信它实际上达到了预期的陈述。
代码
let number = 0;
let count = 0;
// Find Mongoose document
function funcOne(query) {
return new Promise(function (resolve, reject) {
MongooseDocument.find(query)
.exec(function (err, results) {
if (err) {
reject(err);
}
resolve(results);
});
});
}
// Iterate over each document and adjust external value
function funcTwo(documents) {
return new Promise(function (resolve, reject) {
_.each(documents, function (document) {
count += 1;
number += document.otherNumber;
});
if (count >= documents.length) {
resolve({
count,
number,
});
}
});
}
// Chain promises and return result
function funcThree(query) {
return funcOne(query).then(funcTwo).then(function (result) {
console.log("==================");
console.log(result);
return result;
});
}
测试示例
// The expect test below never runs! How do I test this promise? Or
// better yet, how do I get the value returned as a result from the
// promise to test that?
it('should return an object', function () {
funcThree(query).then(function(result) {
return expect(result).to.be.an('object');
});
});