Instalar a través de NPM:
NPM Instalar NodeUnit -g -g
Admite la línea de comando, se ejecuta el navegador. Varias afirmaciones. Exportaciones de exportación modularmente para el método en Node.js. Si exporta módulo.exports para el objeto, el módulo es la base para las pruebas unitarias. Consulte el siguiente código Node.js:
var fs = require ('fs'), global = require ('./ global.js'); var utils = {startwith: function (s1, s) {if (s == null || s == "" || this.length == 0 || s.length> this.length) return false; if (s1.substr (0, s.length) == s) return n. verdadero;},/ * Genere Guid */getGuid: function () {var guía = ""; for (var i = 1; i <= 32; i ++) {var n = math.floor (math.random () * 16.0) .ToString (16); Guid+= n;} return Guid;},/ * Agregar información de log */writelog: function (function (function (function (if) » fs.ReadFilesync (Global.LogFile, "UTF-8"), _ newLog = text? (texto + "/r/n" + log): log; fs.writeFile (global.logfile, _newLog, function (err) {if (err) throw err;});}}; exports.utils = utils;./global.js es un archivo de variable global local. Ahora usamos NodeUnit para probar el código anterior:
var utils = new require ('./ utils.js'); this.testforUtils = {'testgetguid': function (test) {var guía = utils.utils.getGuid (); test.ok (!! Guid, 'getGuid no debe ser nulo.'); test.done ();}, 'testwritelog': function (test) {var) { flag = false; utils.utils.writeLog ("Mensaje de prueba"); flag = true; test.ok (flag, 'writeLog'); test.done ();}, 'testStartWithwords': function (test) {var name = "ad_123"; test.ok (utils.utils.Startwith (nombre ","), ")," Startwith Method debe ser el método Startwith. ok "); test.done ();}};Test.ok también es la afirmación que generalmente decimos. Para los programas de prueba unitarios de NodeUnit, también puede usar Node-Inspector para depurar