2015-06-19 66 views
0

我使用Q.js庫用於模擬異步行爲通過promise使用Q.js承諾爲單元測試:2000毫秒的超時超過

我有一個stubed後端API

class ApiStub { 
    constructor(){ 
     this.deferred = Q.defer(); 
    } 

    post(url, data) { 
     if (data) { 
      this.deferred.resolve(data); 
     } else { 
      this.deferred.reject(new Error("Invalid data")); 
     } 
     return this.deferred.promise; 
    } 
} 

和我想測試一下:

before(() => api = new ApiStub()); 

it("Api test", (done) => { 
     return api.post({}) 
      .then(value => { 
       expect(value).to.exist; 
       done(); 
      }, error => { 
       done(error); 
      }); 
}); 

,但我得到了一個Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

我試圖設置摩卡超時15000ms以上,但它沒有幫助

回答

1

它看起來像你有你的錯誤處理程序作爲與你的測試用例相同的then的一部分。這意味着你不會catch any errors thrown by the expect。試試看看你是否得到不同的錯誤:

it("Api test", (done) => { 
     return api.post({}) 
      .then(value => { 
       expect(value).to.exist; 
       done(); 
      }).catch(error => { 
       done(error); 
      }); 
});