在 chai 中测试 promise 的 return 值
Test return value from promise in chai
我有以下承诺。我知道它们工作正常,因为 funcThree returns 中的 console.log 输出是正确的数据。我如何通过测试证明这一点?
基本上,我如何测试这个承诺?我尝试在下面进行测试,但无论我在其中放入什么(包括 expect(false).to.be.true),它总是 returns 为真。我不相信它实际上达到了期望的陈述。
代码
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');
});
});
When using chai
and chai-as-promised
you need to instruct chai to actually use chai-as-promised
.
var chai = require('chai');
chai.use(require('chai-as-promised');
Also to convert the assertion to return a promise you need to use the keyword eventually
and you need to return the promise back to it()
. When using chai-as-promised
you need to perform the assertion against the actual promise returning function, in this case, funcThree(query)
. This is why you're always having your test function return true, you're not actually waiting for the Promise to resolve and since no error sent back to your it()
then it is assumed succesful. So your test example above should be as follows:
it('should return an object', function () {
return expect(funcThree(query)).to.eventually.be.a('object');
});
You can also make multiple assertions against the same promise using the following syntax
it('should return an object', function () {
var funcThreePromise = funcThree(query);
// Promise below is whatever A+ Promise library you're using (i.e. bluebird)
return Promise.all([
expect(funcThreePromise).to.eventually.be.fulfilled,
expect(funcThreePromise).to.eventually.be.a('object')
]);
});
我有以下承诺。我知道它们工作正常,因为 funcThree returns 中的 console.log 输出是正确的数据。我如何通过测试证明这一点?
基本上,我如何测试这个承诺?我尝试在下面进行测试,但无论我在其中放入什么(包括 expect(false).to.be.true),它总是 returns 为真。我不相信它实际上达到了期望的陈述。
代码
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');
});
});
When using chai
and chai-as-promised
you need to instruct chai to actually use chai-as-promised
.
var chai = require('chai');
chai.use(require('chai-as-promised');
Also to convert the assertion to return a promise you need to use the keyword eventually
and you need to return the promise back to it()
. When using chai-as-promised
you need to perform the assertion against the actual promise returning function, in this case, funcThree(query)
. This is why you're always having your test function return true, you're not actually waiting for the Promise to resolve and since no error sent back to your it()
then it is assumed succesful. So your test example above should be as follows:
it('should return an object', function () {
return expect(funcThree(query)).to.eventually.be.a('object');
});
You can also make multiple assertions against the same promise using the following syntax
it('should return an object', function () {
var funcThreePromise = funcThree(query);
// Promise below is whatever A+ Promise library you're using (i.e. bluebird)
return Promise.all([
expect(funcThreePromise).to.eventually.be.fulfilled,
expect(funcThreePromise).to.eventually.be.a('object')
]);
});