2

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();
Jonas
  • 121,568
  • 97
  • 310
  • 388
Chance
  • 11,043
  • 8
  • 61
  • 84

2 Answers2

3

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

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • Thanks, I had actually figured that part out via your documentation (btw, thanks again for vows, quite awesome) but forgot to edit the question. I've edited though with what you described. In what you described and my edited code (assuming they match), how would you jump back into 'then we will be back on track' with example? Is it possible? – Chance Oct 04 '11 at 12:43
  • I'm basically trying to test the side effects of the async method and would need to be able to regain access to 'example' in order to do so. – Chance Oct 04 '11 at 12:44
  • @Chance you don't jump back. Vows does not allow for this. You can only return one topic. There are some hacks around this – Raynos Oct 04 '11 at 13:39
  • @Chance what do you mean "thanks for vows". I did not write it ;) – Raynos Oct 04 '11 at 13:44
  • @Chance [vows-is](https://github.com/Raynos/vows-is). Also [vows-fluent](https://github.com/Raynos/vows-fluent). However the vows source code is annoying, I may re-write it at some point. – Raynos Oct 04 '11 at 14:51
0

As far as I understand it, topics 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().

benui
  • 6,440
  • 5
  • 34
  • 49
  • Nah, topics can sprawl multiple tests and the result is passed to the next. When you use this.callback as a function, it sets itself (or rather, the next item in the sequence... kinda) as the callback. This is, to my knowledge, the only way to successfully test async behavior. – Chance Oct 04 '11 at 03:33