2017-04-25 40 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