在Vows.js中,如何在经历asynchronouscallback之后恢复原始主题?
说我有以下顺序:
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();
是testing“ and then we will be back on track
”可能击中前面的话题( 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();
主题返回将发送给所有誓言的主题。
在第一个誓言"with an async method"
this.callback
是未定义的,因为一个callback只在一个主题中定义。 在第二个誓言中,参数不是err, result
如果你想做任何asynchronoustesting,你需要为你的主题设置一个函数,并使用this.callback
或从主题返回一个EventEmitter
。
编辑:
你有问题是从誓言返回多个主题。 这不可能。
最简单的解决scheme是返回一个包含两个主题的新主题。 这感觉hackish。
更好的解决scheme是确保您独立testing您的主题。 基本上你的testing“副作用”已经成功发生。 副作用不应该发生
据我了解, topic
是否使用简单的topic: something
或topic: function(...
语法,只执行一次 ,而且随着誓言顺序执行,在第二个例子中,你将会处理修改的example
。
我会采取简单但有点麻烦的路线分裂两个testing到子上下文和有两个topic: new Example()
实例topic: new Example()
。