gpt4 book ai didi

javascript - 使用 Q.js Promise 进行单元测试 : timeout of 2000ms exceeded

转载 作者:行者123 更新时间:2023-12-03 09:48:27 25 4
gpt4 key购买 nike

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

我有一个已 stub 的后端 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);
});
});

但我收到错误:超时超过 2000 毫秒。确保在此测试中调用done()回调。

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

最佳答案

看起来您的错误处理程序是与您的测试用例相同的 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);
});
});

关于javascript - 使用 Q.js Promise 进行单元测试 : timeout of 2000ms exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30948210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com