1

I'm completely new to node.js testing, maybe you can help me out: I want to do some more or less simple tests for my express webapp using vows and tobi (for example testing if the login route works)

var vows   = require('vows');
var assert = require('assert');
var tobi   = require('tobi');

var browser = tobi.createBrowser(8080, 'localhost');

vows.describe('mytest').addBatch({

    'GET /': {
        topic: function() {

            browser.get("/", this.callback);

        },
        'has the right title': function(res, $) {

            $('title').should.equal('MyTitle');

        }
    }


}).export(module);

and I get this:

♢ mytest

GET /
    ✗ has the right title
      » expected { '0': 
    { _ownerDocument: 

    [....lots of stuff, won't paste it all.....] 

    Entity: [Function: Entity],
    EntityReference: [Function: EntityReference] } },
    selector: ' title' } to equal 'MyTitle' // should.js:295

✗ Broken » 1 broken (0.126s)

I can't recognize what's wrong from this output but I'm guessing it has someone to do with callbacks. I'm also fairly new to the async style of programming in node.js.

Kieran
  • 2,554
  • 3
  • 26
  • 38
toxinlabs
  • 942
  • 1
  • 10
  • 14

1 Answers1

1

vows expects the first argument to the callback to be an error. If it's not null or undefined it thinks something's wrong. You'll have to wrap the callback into an anonymous function that calls it with null as its first argument.

vows.describe('mytest').addBatch({

    'GET /': {
        topic: function() {
            var cb = this.callback;
            browser.get("/", function() {
                var args = Array.prototype.slice.call(arguments);
                cb.apply(null, [null].concat(args));
            });

        },
        'has the right title': function(err, res, $) {

            $('title').should.equal('MyTitle');

        }
    }


}).export(module);
fent
  • 17,861
  • 15
  • 87
  • 91
  • 1
    vows is kind of weird. If you have one argument in your callback and call it with something in the first argument, it will think it's an error and tell you there is an error. If you have more than one it thinks you want to handle the err yourself and doesn't throw an error. Try adding an extra err argument in the beginning to your original code. – fent Jan 10 '12 at 19:50
  • And this is not well documented by vows, I'm not quite sure how it works. It just something you encounter as you work with it. – fent Jan 10 '12 at 19:51
  • adding an err argument in the beginning throws an error too: "TypeError: undefined is not a function". hmmm ... maybe you have a recommendation for a testing node module? i need the client-side capabilities from zombie or tobi (.submit,.fill, etc.) so something which goes well with one of these two would be great. also a good documentation would be nice.. i think most of the nodejs testing modules lack a good documentation. it's either that or it's because of my poor programming skills – toxinlabs Jan 10 '12 at 20:12
  • You could try printing out `arguments` to see what you get. As for testing module, I prefer [mocha](http://visionmedia.github.com/mocha/). It's much more flexible than vows, it's actively maintained, and so much easier to share variables through async functions. – fent Jan 10 '12 at 23:25