【问题标题】:Webapp testing with Vows and Tobi使用 Vows 和 Tobi 进行 Webapp 测试
【发布时间】:2012-01-09 23:29:11
【问题描述】:

我对 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 中的异步编程风格也很陌生。

【问题讨论】:

    标签: node.js express vows


    【解决方案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);
    

    【讨论】:

    • 誓言有点奇怪。如果你的回调中有一个参数并在第一个参数中使用某些东西调用它,它会认为这是一个错误并告诉你有一个错误。如果您有多个,它认为您想自己处理错误并且不会引发错误。尝试在原始代码的开头添加一个额外的 err 参数。
    • 这并没有被誓言很好地记录下来,我不太确定它是如何工作的。它只是您在使用它时遇到的东西。
    • 在开头添加 err 参数也会引发错误:“TypeError: undefined is not a function”。嗯......也许你有一个测试节点模块的建议?我需要来自zombie 或tobi 的客户端功能(.submit、.fill 等),因此与这两者之一相得益彰的东西会很棒。一个好的文档也很好。我认为大多数 nodejs 测试模块都缺乏一个好的文档。要么是这样,要么是因为我的编程能力差
    • 您可以尝试打印出arguments 看看您会得到什么。至于测试模块,我更喜欢mocha。它比 vows 灵活得多,它得到积极维护,并且通过异步函数共享变量要容易得多。
    猜你喜欢
    • 2011-10-18
    • 2011-11-03
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多