在 Vows.js 中,如何在经历异步回调后恢复到原始主题?
假设我有以下序列:
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();
测试“然后我们将回到正轨
”是否可以与之前的主题(示例
)相匹配?
编辑
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();
Say I have the following sequence:
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();
Is the test "and then we will be back on track
" possible to hit with the topic (Example
) from before?
EDIT
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主题返回将发送给您所有誓言的主题。
在第一个誓言中,
“使用异步方法”
this.callback
未定义,因为回调仅在主题中定义。在第二个誓言中,参数是example
而不是err, result
如果您想进行任何异步测试,您需要为您的主题设置一个函数并使用
this。回调
或从主题返回一个EventEmitter
。编辑:
您遇到的问题是从誓言返回多个主题。这是不可能的。
最简单的解决方案是返回一个新主题,该主题是两个主题的数组。这感觉很黑客。
更好的解决方案是确保单独测试您的主题。基本上你的测试“副作用”已经成功发生。副作用不应该发生
A topic returns a topic that will be send to all your vows.
In that first vow
"with an async method"
this.callback
is undefined because a callback is only defined in a topic. In the second vow the arguments areexample
noterr, result
If you want to do any async testing you need to set a function for your topic and use
this.callback
or return anEventEmitter
from the topic.Edit:
The issue you have is returning multiple topics from vows. This is not possible.
The easiest solution is to return a new topic that is an array of two topics. This feels hackish.
The better solution is to make sure that you test your topics in isolation. Basically your testing that "side-effects" have successfully occurred. Side effects should never occur
据我了解,
topic
无论是使用简单的topic: Something
还是topic: function(...
语法,都只执行一次,当誓言按顺序执行时,在第二个示例中,您将处理修改后的示例
。将两个测试拆分为子上下文并拥有两个
topic 实例:新的示例()。
As far as I understand it,
topic
s whether using the simpletopic: something
ortopic: function(...
syntax, are executed only once. And as vows are executed in sequence, in your second example, you'll be dealing with the modifiedexample
.I'd take the simple but kind of annoying route of splitting the two tests into sub-contexts and having two instances of
topic: new Example()
.