0
我有以下承諾。我知道他們工作正常,因爲funcThree中的console.log輸出返回正確的數據。我如何通過測試證明這一點?來自承諾的測試返回值chai
基本上,我該如何測試這個承諾?我試圖在下面進行測試,但無論我放在那裏(包括expect(false).to.be.true),它總是返回true。我不相信它實際上達到了預期的表述。
CODE
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');
});
});