3

如何在 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()
4

1 回答 1

2

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

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

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

我这样做了(在咖啡脚本中):

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

模型.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 未运行/不可连接时,测试会像您所说的那样失败。因此,您可能还想检查您的连接字符串。

于 2012-01-19T08:22:04.217 回答