0

ノードユニットとマングースで非同期結果を返すにはどうすればよいですか? 私の失敗をより簡単に示すために、非常にわずかに変更しました。

var mongoose = require('mongoose');

var db;

module.exports = {
    setUp: function(callback) {
        try {
            //db.connection.on('open', function() {
            mongoose.connection.on('open', function() {
                console.log('Opened connection');
                callback();
            });

            db = mongoose.connect('mongodb://localhost/test_1');
            console.log('Started connection, waiting for it to open');
        } catch (err) {
            console.log('Setting up failed:', err.message);
            test.done();
            callback(err);
        }
    },

    tearDown: function(callback) {
        console.log('In tearDown');
        try {
            console.log('Closing connection');
            db.disconnect();
            callback();
        } catch (err) {
            console.log('Tearing down failed:', err.message);
            test.done();
            callback(err);
        }
    },

    test1: function(test) {
        test.ifError(null);
        test.done();
    },
    test2: function(test) {
        test.ifError(null);
        test.done();
    }
};

これを nodeunit で実行すると、次のようになります。

stam2_test.js
Started connection, waiting for it to open
Opened connection
In tearDown
Closing connection
✔ test1
Started connection, waiting for it to open
Opened connection

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

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

詳細情報: setUp/tearDown で mongo を使用せず、カウンターを増やすなどのテスト コードのみを使用する場合、すべて機能します。テストが 1 つしかない場合は、すべてが機能します。別のテストを追加し、セットアップでmongoを使用すると一貫して失敗するため、セットアップで何か間違ったことをしていると思います。

前もって感謝します。

4

1 に答える 1