getById()返回未定义的Promise

时间:2017-04-25 10:17:54

标签: node.js promise sequelize.js models chai

我的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断言它的结果?

1 个答案:

答案 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
  });