gpt4 book ai didi

node.js - 在 Vows.js 中,如何在完成异步回调后返回到原始主题?

转载 作者:搜寻专家 更新时间:2023-11-01 00:46:54 24 4
gpt4 key购买 nike

假设我有以下序列:

vows.describe('Example').addBatch({
'An example' : {
topic: new Example(),
'with an async method' : function(example) {
example.asyncMethod(this.callback);
},
'will do some magic' : function(err, result) {
assert.equal(result.magical, true);
},
'and then we will be back on track' : function(example) {
assert.isTrue(example.backOnTrack);
}
}
}).run();

测试“然后我们将回到正轨”是否可以与之前的主题(Example)相吻合?

编辑

vows.describe('Example').addBatch({
'An example' : {
topic: function(example){ // <- where example is a parent topic
example.asyncMethod(this.callback);
},
'callback after an async method will do some magic' : function(err, result) {
assert.equal(result.magical, true);
},
'and then we will be back on track' : function(example) {
assert.isTrue(example.backOnTrack);
}
}
}).run();

最佳答案

主题返回将发送给您所有誓言的主题。

在第一个誓言中,“使用异步方法” this.callback 是未定义的,因为回调仅在主题中定义。在第二个誓言中,参数是 example 而不是 err, result

如果您想进行任何异步测试,您需要为主题设置一个函数并使用 this.callback 或从主题返回一个 EventEmitter

编辑:

您遇到的问题是从 vows 返回多个主题。这是不可能的。

最简单的解决方案是返回一个包含两个主题的数组的新主题。这感觉很骇人听闻。

更好的解决方案是确保单独测试主题。基本上,您对“副作用”的测试已成功发生。副作用永远不应该发生

关于node.js - 在 Vows.js 中,如何在完成异步回调后返回到原始主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7640946/

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