2017-04-25 86 views
0

我在Sequelize模型下面的类方法:getById()返回undefined无极

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

现在我想测试我的方法与薛宝钗。但现在,当我做Conference.getById(confereceId)我得到以下回:

Promise { 
    _bitField: 0, 
    _fulfillmentHandler0: undefined, 
    _rejectionHandler0: undefined, 
    _promise0: undefined, 
    _receiver0: undefined } 

这是正确的,如何认定它与薛宝钗的结果呢?

+1

看看['柴AS-promised' ](https://github.com/domenic/chai-as-promised)。 – robertklep

回答

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 
    }); 
+1

如果'then()'中的任何断言失败,它们也会触发'catch()',这可能导致混淆错误。 – robertklep

+1

这是真的,我编辑答案使用'.then(onFulfilled,onRejected)'而不是'.then(onFulfilled).catch(onRejected)'来解决这个问题。 –

+1

是的,我看到了+1:D – robertklep