質問

node.jsとnodeunitでスピードを上げようとしていますが、通話が表示されないnodeunitに問題を見つけています test.done() テストの1つで。

コード:

// Added for clarity.
var client = require("restify").createJsonClient({
    "version": "*",
    "url": "http://localhost:" + server.Port
});

exports["tests"] = {
    "setUp": function (callback) {
        server.StartServer();
        callback();
    },
    "tearDown": function (callback) {
        callback();
    },
    "CanIHaveSomeTeaPlease?": function (test) {
        test.expect(4);
        client.get("/tea", function (err, req, res, data) {
            test.equal(err.statusCode, 418, "Expected ImATeapot Error.");
            test.equal(err.message, "Want a biscuit?", "Expected to be asked if I want a buscuit.");
            test.equal(err.restCode, "ImATeapotError");
            test.equal(err.name, "ImATeapotError");
            test.done();
        });
    },

    // Note: I expect this test to fail as it is a copy of the above
    //       test on a different url that doesn't return the ImATeapot
    //       HTTP error. But it doesn't look like it's detecting it
    //       properly.

    "TakeThisInfo": function (test) {
        test.expect(4);
        client.put("/push", {
            "hello": "world"
        }, function (err, req, res, data) {
            test.equal(err.statusCode, 418, "Expected ImATeapot Error.");
            test.equal(err.message, "Want a biscuit?", "Expected to be asked if I want a buscuit.");
            test.equal(err.restCode, "ImATeapotError");
            test.equal(err.name, "ImATeapotError");
            test.done();
        });
    }
};

出力:

FAILURES: Undone tests (or their setups/teardowns):
- tests - TakeThisInfo

To fix this, make sure all tests call test.done()

私はそれが愚かなものであることを望んでいます。

バージョン: -

Node: 0.10.21
NPM: 1.3.11
Nodeunit: 0.8.2
Grunt-CLI: 0.1.10
Grunt: 0.4.1
役に立ちましたか?

解決

まず、私はあなたのコードに「サーバー」が何であるかわかりませんが、それが非同期であると期待しているので、セットアップ関数にこのようなものを持っていることができます。

function (callback) {
  server.StartServer(function(){
    callback();
  });
}

第二に、そのnodeunitを存在させてください スタートアップと、すべてのテストの後と前に分解機能を実行します ですから、サーバーを2回起動しているのではないかと思います(裂け下がっていると、実際に閉じていないように)。

他のヒント

私はこの問題をいじって過去数時間を費やしましたが、明らかになったのは、Nodeunitには、IOまたはSettimeOutタイプのプロセスによって後でトリガーされる関数で例外をキャッチして表示する能力がないことです。 JavaScriptの実行方法を考えると、これは驚くことではありません。例外がないと確信したらすべてが機能しますが、コードにエラーが発生した場合、「テストを元に戻す」メッセージが表示されます。これが私の問題を解決するためにしたことです(例としてRESTIFYルートを使用して):

function myRoute(req, res, next) {
    try {
        // your code goes here...
    }
    catch (err) {
        // write it to the console (for unit testing)
        console.log(err);
        // pass the error to the next function.
        next(err);
    }
}

この方法で問題を理解したら、もっと明確になり、すべてのテストを渡すことができたため、それを修正しました!

私はあなたが実際に電話していないのではないかと思います test.done() その2番目のテストで。 a console.log() そこに電話して、あなたが実際にその呼び出しをしていることを確認してください。

FWIW、私はあなたのテストの簡略化されたバージョンを以下で使用して、説明された問題を非難しました。省略した場合 on('error', function() {...}) ハンドラー、2番目のテストが完了しません。したがって、私の理論はあなたの理論です /push エンドポイントは、Restifyモジュールで異なる動作をトリガーしています。つまり、あなたです 承知しました Restifyは、anでコールバックを呼び出しています err そこに財産、またはそれは何か違うことをしているのですか? ...たとえば、次のようなイベントを発するように http.get 以下にあります。

var http = require('http');

exports.test1 = function (test) {
  test.expect(1);
  http.get({hostname: "www.broofa.com", path: "/"}, function (res) {
    test.equal(res.statusCode, 200, 'got 200');
    test.done();
  });
};

exports.test2 = function (test) {
  test.expect(1);
  http.get({hostname: "www.no-such-domain.com", path: "/"}, function (res) {
    test.equal(res.statusCode, 200, 'got 200');
    test.done();
  }).on('error', function() {
    // Comment line below out to repro the "Undone tests" error
    test.done();
  });
};

私は、セットアップでサーバーを独自のプロセスに分岐させ、それを分解で殺すことで、それを回避しています。この問題は、シャットダウンではなく、サーバーが作成されていることに関係していると考えています。 @matteofigusに感謝します。

var cp = null; // child process
exports["tests"] = {
    "setUp": function (callback) {
        cp = fork("./lib/server.js", {"silent": true});
        callback();
    },
    "tearDown": function (callback) {
        cp.kill("SIGHUP");
        callback();
    },
    "CanIHaveSomeTeaPlease?": function (test) {
        test.expect(4);
        client.get("/tea", function (err, req, res, data) {
            test.equal(err.statusCode, 418, "Expected ImATeapot Error.");
            test.equal(err.message, "Want a biscuit?", "Expected to be asked if I want a buscuit.");
            test.equal(err.restCode, "ImATeapotError");
            test.equal(err.name, "ImATeapotError");
            test.done();
        });
    },
    "TakeThisInfo": function (test) {
        test.expect(1);
        client.put("/push", {
            "hello": "world"
        }, function (err, req, res, data) {
            test.ok(false);
            test.done();
        });
    }
};
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top