2015-06-19 67 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); 
      }); 
});