gpt4 book ai didi

node.js - 使用 Nodejs Vows (BDD) 测试 Bluebird Promise

转载 作者:太空宇宙 更新时间:2023-11-03 22:00:50 24 4
gpt4 key购买 nike

我在如何使用 Vows 正确构建 Promise-returning API 测试方面遇到了困难,例如

topic:function() { return myfunc() { /* returns a Bluebird Promise */ } },
'this should keep its promise':function(topic) {
var myfunc = topic;
myfunc()
.then(function(result) {
assert(false);
})
.catch(function(error) {
assert(false);
})
.done();
}

我的誓言永不落空。这是我第一次尝试用誓言来检验 promise 。希望熟悉这方面的人帮忙。

提前谢谢。

恩里克

最佳答案

由于与 Mocha - Vows 等库不同,尚不支持测试 Promise,因此我们使用其接受回调的常规异步测试格式:

topic:function() { return myfunc() { /* returns a Bluebird Promise */ } },
'this should keep its promise':function(topic) {
var myfunc = topic;
myfunc() // call nodeify to turn a promise to a nodeback, we can chain here
.nodeify(this.callback); // note the this.callback here
}

这是摩卡咖啡的样子:

describe("Promises", function(){
it("topics", function(){
return myfunc(); // chain here, a rejected promise fails the test.
});
})

关于node.js - 使用 Nodejs Vows (BDD) 测试 Bluebird Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465564/

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