In Vows.js how do you revert back to the original topic after going through an asynchronous callback?
Pregunta
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();
Solución
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 are example
not err, result
If you want to do any async testing you need to set a function for your topic and use this.callback
or return an EventEmitter
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
Otros consejos
As far as I understand it, topic
s whether using the simple topic: something
or topic: function(...
syntax, are executed only once. And as vows are executed in sequence, in your second example, you'll be dealing with the modified example
.
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()
.