我的Sequelize模型中有以下类方法:
getById(id) {
return new Promise((resolve, reject) => {
var Conference = sequelize.models.conference;
Conference.findById(id).then(function(conference) {
if (_.isObject(conference)) {
resolve(conference);
} else {
throw new ResourceNotFound(conference.name, {id: id});
}
}).catch(function(err) {
reject(err);
});
});
}
现在我想用chai测试我的方法。但现在当我Conference.getById(confereceId)
时,我得到了以下回复:
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
这是对的,我如何用chai断言它的结果?
答案 0 :(得分:3)
您的Conference.getById(confereceId)
调用会返回一个承诺,因此您应首先通过then
解决承诺,并使用chai
声明其结果,如下所示:
const assert = require('chai').assert;
Conference
.getById(confereceId)
.then(conf => {
assert.isObject(conf);
// ...other assertions
}, err => {
assert.isOk(false, 'conference not found!');
// no conference found, fail assertion when it should be there
});