如何使用节点单元和mongoose返回异步结果?

时间:2011-11-23 13:41:59

标签: node.js mongoose nodeunit

如何在nodeunit和mongoose中获得异步结果?我已经尝试了以下代码,似乎挂起数据库回调永远不会返回结果或错误。

mongoose = require "mongoose"
models = require "../Services/models"
Task = models.Task

module.exports =
setUp: (callback) ->
    try
        @db = mongoose.connect "myConnString"
        console.log 'Started connection, waiting for it to open'


        @db.connection.on 'open', () ->
            console.log 'Opened connection'
            callback()
    catch err
          console.log 'Setting up failed:', err.message
tearDown: (callback) ->
    console.log 'In tearDown'
    try
        console.log 'Closing connection'
        @db.disconnect()
        callback()
    catch err
        console.log 'Tearing down failed:', err.message
"get tasks" : (test) ->
    console.log 'running first test'
    Task.find {}, (err, result) ->
        if not err
            console.log 'results' + result
            test.ok(result)
        else
            console.log 'error' + err   
        test.ifError(err)
        test.done()

1 个答案:

答案 0 :(得分:2)

我已将测试脚本从Coffee Script移植到JavaScript并在NodeUnit中运行,见下文。

虽然我改变了两件事。首先,而不是:

@db.connection.on 'open', () ->

我做了这个(在Coffee Script中):

mongoose.connection.on 'open', () ->

其次,我已撤销订单或注册回调并建立连接。

生成的JavaScript:

var mongoose = require('mongoose');
var models = require('./models');
var Task = models.Task;

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);
        }
    },

    tearDown: function(callback) {
        console.log('In tearDown');
        try {
            console.log('Closing connection');
            db.disconnect();
            callback();
        }

        catch (err) {
            console.log('Tearing down failed:', err.message);
        }
    },

    getTasks: function(test) {
        console.log('running first test');
        Task.find({}, function (err, result) {
            if (!err) {
                console.log('results' + result);
                test.ok(result);
            } else {
                console.log('error' + err);
            }

            test.ifError(err);
            test.done();
        });
    }
};

Models.js

var mongoose = require('mongoose');

var TaskSchema = new mongoose.Schema({
    field1: String,
    field2: Number
});

module.exports.Task = mongoose.model('Task', TaskSchema);

结果输出:

$ ~/node_modules/nodeunit/bin/nodeunit test.js 

test.js
Started connection, waiting for it to open
Opened connection
running first test
results
In tearDown
Closing connection
✔ getTasks

OK: 2 assertions (198ms)

我必须注意,当MongoDB没有运行/不可连接时,测试失败就像你说的那样。所以你可能也想检查你的连接字符串。