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

最佳答案

您的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
  });

07-28 11:50