1

我对 node.js 测试完全陌生,也许你可以帮助我:我想使用 vows 和 tobi 为我的 express 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 中的异步编程风格也很陌生。

4

1 回答 1

1

vows 期望回调的第一个参数是一个错误。如果它不是 null 或 undefined,它会认为有问题。您必须将回调包装到一个匿名函数中,该函数以 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);
于 2012-01-10T07:03:38.513 回答