簡體   English   中英

使用Mocha測試Promise時出現未捕獲(In Promise)錯誤

[英]Uncaught (in promise) error when testing promise with Mocha

我很難測試基於promise的功能。 我正在使用Mocha作為測試框架,並使用chai庫作為斷言框架。 我是摩卡咖啡和蔡的新手。 我遇到的問題很少,我也不知道我的代碼有問題,也許我的測試方法完全錯誤,也許有人幫助了他們。

我遇到了Uncaught(承諾)錯誤,這是預期的,但是實際上我不知道我的方式是否是測試這些功能的正確方式。

這是我的returnResult函數,它解析一個值->一個字符串

var returnResult = function (stateParamName, stateParamValue) {

 return new Promise((resolve, reject) => {
   peer3.get({ path: { equals: 'todo/#0' } }).then(function (results) {
    console.log(stateParamName + ': ' + results[0].value[stateParamName])
    resolve(results[0].value[stateParamName]);
  });
});
}

這是我的摩卡咖啡測試

 describe("Call Tests Suite", function () {
  describe("Basic Call Test", function () {
  it("Call status for both side should be IN CALL", function () {
  peer3.call('login/add', ['testaccount1'])
    .then(() => peer3.call('todo/makeCall1', ['testaccount2@testdomain.com']))
    .then(() => checkResult('state_term', 'RINGING'))
    .then((interval) => { clearInterval(interval); peer3.call('todo/answerCall2', ['empty']) })
    .then(() => checkResult('state_term', 'IN_CALL'))
    .then((interval) => clearInterval(interval))
    //.then(console.log('test sonucu: ' + returnResult('state_term', 'IN_CALL')))
    .then(returnResult('state_term', 'IN_CALL'))
    .then((result) => expect(result).to.equal('IN_CALL'))        
   });

 });

如您所見,我僅將assert用於最后的結果。 也許我應該將整個測試作為promise函數進行測試。 有人可以幫我嗎?

我不知道您的錯誤來自哪里。 但是,您的代碼中有很多可以改進的地方,並且可能會導致您進行更好的調試,因此您可以找到錯誤:

1-您應在.catch鏈的末尾添加.catch處理程序。 未捕獲的錯誤”是指:您then一個處理程序中有一個錯誤,但是沒有被catch 您應該添加catch在列車結束通話:

describe("Call Tests Suite", function () {
describe("Basic Call Test", function () {
it("Call status for both side should be IN CALL", function () {
peer3.call('login/add', ['testaccount1'])
    .then(() => peer3.call('todo/makeCall1', ['testaccount2@testdomain.com']))
    .then(() => checkResult('state_term', 'RINGING'))
    .then((interval) => { clearInterval(interval); peer3.call('todo/answerCall2', ['empty']) })
    .then(() => checkResult('state_term', 'IN_CALL'))
    .then((interval) => clearInterval(interval))
    //.then(console.log('test sonucu: ' + returnResult('state_term', 'IN_CALL')))
    .then(returnResult('state_term', 'IN_CALL'))
    .then((result) => expect(result).to.equal('IN_CALL'))
    .catch(err => {
      console.log(err);//This will allow you better debugging.
    })       
});

});

好的,現在,我們必須記住您的代碼是異步的。 但是, 默認情況下 ,mocha it函數是同步的:它們不會等待異步代碼執行。

為了告訴mocha您的測試是異步的,您必須將參數傳遞給測試。 此參數是一個函數,通常稱為done ,在測試完成時必須顯式調用。 否則,您的測試將在到達代碼的異步部分之前完成,通常會給您帶來誤報。

describe("Call Tests Suite", function () {
describe("Basic Call Test", function () {
it("Call status for both side should be IN CALL", function (done) {
peer3.call('login/add', ['testaccount1'])
    .then(() => peer3.call('todo/makeCall1', ['testaccount2@testdomain.com']))
    .then(() => checkResult('state_term', 'RINGING'))
    .then((interval) => { clearInterval(interval); peer3.call('todo/answerCall2', ['empty']) })
    .then(() => checkResult('state_term', 'IN_CALL'))
    .then((interval) => clearInterval(interval))
    //.then(console.log('test sonucu: ' + returnResult('state_term', 'IN_CALL')))
    .then(returnResult('state_term', 'IN_CALL'))
    .then((result) => expect(result).to.equal('IN_CALL'))
    .then( () => {
        done(); //dont use .then(done) or things may break due to extra 
        parameter
    })
    .catch( err => {
         console.log(err);
         done(err); //passing a parameter to done makes the test fail.
    })       
});

});

不過,您的代碼仍有問題,我們必須解決。 then方法將函數作為參數。 但是,在此行中:.then(returnResult('state_term','IN_CALL'))

您正在傳遞給它一個對returnResult('state_term', 'IN_CALL')的調用,該返回不是函數,而是一個promise。 您應該改為傳遞一個函數-然后返回promise-:

.then(() => returnResult('state_term', 'IN_CALL')) //now the parameter to 
       //then is a function that return a Promise, not a Promise itself.

另外,正如您在注釋中所告訴的那樣,您將顯式返回一個新的Promise,該Promise將一個Promise包裝在return result函數中:根本不需要,並且該函數可以這樣編寫:

var returnResult = function (stateParamName, stateParamValue) {
    return peer3.get({ path: { equals: 'todo/#0' } }).then(function (results) {
        console.log(stateParamName + ': ' + results[0].value[stateParamName]);
        return(results[0].value([stateParamName]));
    });
}

但是,您的代碼中有很多事情看起來真的很奇怪(我根本不確定為什么存在setinterval調用),因此我非常有信心測試不會按您預期的那樣進行。 您應該首先熟悉Promises和異步Mocha測試,而不要嘗試測試很長的異步操作序列。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM