Install via NPM:
NPM Install Nodeunit-g
Support Command line, browser run. Various assertions. In Node.js exports for method export, if object export module.exports, module child is the basis of unit test, look at the following Node.js code:
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
true;
else return
false;
return true;
},
/* Generate GUID
/getguid:function () {
var guid = ' ";
for (var i = 1; I <= i++) {
var n = math.floor (Math.random () * 16.0). toString ();
GUID + N;
}
return GUID;
/* Add log information *
/writelog:function (log) {
if (!log) return;
var text = Fs.readfilesync (global.logfile, "Utf-8"),
_newlog = text? (text + "\ r \ n" + log): log;
Fs.writefile (global.logfile, _newlog, function (err) {
if (err) throw err;
});
Exports.utils=utils;
./global.js is a local global variable file, and we now use Nodeunit to test the Node.js code for the above code:
var utils=new require ('./utils.js ');
This. Testforutils = {
' Testgetguid ': function (test) {
var guid=utils.utils.getguid ();
Test.ok (!!) GUID, ' getguid should not to be null ');
Test.done ();
},
' Testwritelog ': function (test) {
var flag=false;
Utils.utils.writeLog ("test Message");
Flag=true;
Test.ok (flag, ' writelog ');
Test.done ();
},
' Teststartwithwords ': function (test) {
var name= ' ad_123 ';
Test.ok (Utils.utils.startWith (name, "Ad_"), "Startwith method should to be OK");
Test.done ();
}
;
Test.ok is also usually the assertion we say. For Nodeunit unit test programs, you can also use Node-inspector to debug