1

私はnode.jsテストにまったく慣れていません。多分あなたが私を助けてくれるかもしれません.vowsとtobiを使用して、エクスプレスWebアプリケーションの多かれ少なかれ簡単なテストを行いたいです(たとえば、ログインルートが機能するかどうかのテスト)

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 に答える