0

我正在尝试使用nodeunit为我的简单数据库层SimpleDbLayer 编写一组测试单元。我发现它相当困难。

问题:

1)我想连接到数据库一次,并使用该连接。我不能真正使用 setUp 和 tearDown,因为它们在每次测试之前和之后运行。作为“解决方案”,我使用模块范围的变量作为范围(参见下面的代码)

2)我的简单数据库层有几个数据库驱动程序。MongoMixin 只是其中之一——我正在开发 MariaMixin 和 PostgresMixin。理想情况下,我会为每个驱动程序运行这些测试,看看它们是否有效。有没有一个好的,简单的方法来做到这一点?

3)我遇到了我的测试代码出错的情况,我得到的只是关于 test.done 没有被检测到的投诉。这里有什么我需要注意的诡计吗?

4)我做错了什么可怕的事情吗?(见代码)

var 
  dummy

, declare = require('simpledeclare')
, SimpleDbLayer = require('./SimpleDbLayer')

, MongoMixin = require('./MongoMixin.js')

, mw = require('mongowrapper')
, async = require('async')
;

var db, layer;


exports.createLayer = {

  databaseConnect: function( test ){
    mw.connect('mongodb://localhost/hotplate', {}, function( err, returnedDb ){
      test.ifError( err );
      db = returnedDb;
      test.done( null );
    });
  },


  makeLayer: function( test ){

    var C = declare( [ SimpleDbLayer, MongoMixin ] );
    layer = new C( 'test', {  name: true, surname: true, age: true }, db );

    test.ok( layer );
    test.done(); 
  },

  deleteAll: function( test ){
    layer.delete( { }, { multi: true }, function( err, howmany ){
      test.ifError( err );
      test.done();
    });
  },

  insert: function( test ){
    var people = [
      { name: 'Chiara',    surname: 'Mobily',     age: 24 },
      { name: 'Tony',      surname: 'Mobily',     age: 37 },
      { name: 'Sara',      surname: 'Connor',     age: 14 },
      { name: 'Daniela',   surname: 'Mobily',     age: 64 },
    ];

    returnedPeople = [];

    var functions = [];

    // Populate the database
    people.forEach( function( person ){

      functions.push( function( done ){
        layer.insert( person, { returnRecord: true }, function( err, person ){
          test.ifError( err );
          returnedPeople.push( person );
          done( null );
        })
      })

    })

    async.series( functions, function( err, res ){
      test.ifError( err );
      test.done();
    });

  },

  databaseDisconnect: function( test ){
    db.close();
    test.done();
  },

} 
4

1 回答 1

0

自己回答。解决方案很简单:对于每个驱动程序,这是 test.js 文件:

var driver = require('./specificDriver'); var simpledblayerTests = require("./lib/simpledblayer/test.js");

var tests = simpledblayerTests.get(

  function getDbInfo( done ) {
    mw.connect('mongodb://localhost/tests', {}, function( err, db ){
      if( err ){
        throw new Error("MongoDB connect: could not connect to database");
      } else {
        done( null, db, driver );
      }
    });
  },

  function closeDb( db, done ) {
    db.close( done );
  }
);


for(var test in tests) {
    exports[ test ] = tests[ test ];
}

基本上,get()实际的“主”模块(稍后会详细介绍)导出的函数需要两个参数:打开和关闭数据库连接的两个函数。

get()函数返回一堆准备导出的函数——构成单元测试的函数。

主模块将具有以下内容:

exports.get = function( getDbInfo, closeDb ){

  var tests;
  var g = {};

  var startup = function( test ){
    var self = this;

    test.doesNotThrow( function(){

      getDbInfo( function( err, db, driver ){
        if( err ){
          throw( new Error("Could not connect to db, aborting all tests") );
          process.exit();
        }

        // Set the important g.driver variables (db and driver)
        g.db = db;
        d.driver = driver

        test.done();
      });
    });
  }


  var finish = function( test ){
    var self = this;
    closeDb( g.db, function( err ){
      if( err ){
        throw( new Error("There was a problem disconnecting to the DB") );
      }
      test.done();
    });
  };

  tests = {

    startup: startup,

    // ...
    // Your tests here. All functions will have access to g.db and g.driver 
    // ...

    finish: finish

  }

  return tests;
}

这样,每个模块都将运行相同的测试。但是,driver连接/断开数据库的变量和函数将很简单。

使用真实代码查看它的实际效果:

于 2013-11-14T07:27:04.043 回答