2016-04-19 128 views
0

我有下面的代碼,我需要測試:柴-AS-承諾:處理錯誤時,承諾會拋出錯誤

function A(param){ 
    // Process param 
    return B(param) 
    .catch(function(err){ 
     //Process err 
     throw new customError(err); // throw a custom Error 
    }) 
    .then(function(response){ 
     // Handle response and return 
     return {status: 'Success'} 
    }) 
} 

爲了測試它,我用下面的代碼片段:

return expect(A(param)) 
      .to.eventually.have.property('status', 'Success'); 

這當代碼沒有在函數A或B中中斷時工作正常,但是當它發生時,測試用例失敗,並且grunt-mocha無法退出,我認爲這是由於它仍在等待A解決。

如果我在catch塊中添加return而不是throw,grunt-mocha會退出。有沒有更好的方法來測試這種情況?

回答

0

捕捉是爲了捕捉和糾正任何錯誤,而不是重新拋出它們。因此,返回錯誤或拒絕的承諾是處理此問題的正確方法。

+0

'Promise.reject'聽起來是正確的事情。謝謝 –