使用Vows和Tobi进行Webapp测试

时间:2012-01-09 23:29:11

标签: node.js express vows

我对node.js测试完全不熟悉,也许你可以帮助我: 我想使用誓言和tobi为我的快速webapp做一些或多或少的简单测试(例如测试登录路由是否有效)

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);

我得到了这个:

♢ 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)

我无法识别这个输出有什么问题,但我猜它有回应的人。我对node.js中的异步编程风格也很陌生。

1 个答案:

答案 0 :(得分:1)

vows期望回调的第一个参数是一个错误。如果它不是null或未定义它认为有问题。您必须将回调包装到一个匿名函数中,该函数使用null作为其第一个参数来调用它。

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);