【问题标题】:Using Q.js promise for unit-tests: timeout of 2000ms exceeded使用 Q.js 承诺进行单元测试:超过 2000 毫秒的超时
【发布时间】:2015-06-19 22:33:02
【问题描述】:

我正在使用 Q.js 库通过 promise 模拟异步行为

我有一个存根后端 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.

我尝试将 mocha 超时设置超过 15000 毫秒,但没有帮助

【问题讨论】:

    标签: javascript unit-testing asynchronous mocha.js q


    【解决方案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);
                });
     });
    

    【讨论】:

      猜你喜欢
      • 2015-02-05
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 2017-09-13
      • 2021-12-31
      • 2017-04-15
      相关资源
      最近更新 更多