通过新的安装:

新安装nodeunit g

支持命令行,浏览器运行。各种各样的说法。在Node.js,模块化可以出口的方法,如果对象是出口module.exports,模块单元测试的基础。看看下面的js代码:


VaR FS =需要('fs),
全局要求=;
VaR工具= { {
开始:功能(S1,S){
If (s = = null s = = this.length || || || s.length > this.length = = 0)
返回false;
如果(s1.substr(0,s.length)= S)
返回true;
其他的
返回false;
返回true;
},
生成GUID
GetGuid:函数(){
var;
对于(var i = 1;i < 32;i + +){
var n = math.floor(Math.random)*(16)ToString(16);
GUID = n;
}
返回GUID;
},
添加日志信息
写登录日志:功能(日志){
如果(!日志)返回;
var text = fs.readfilesync(global.logfile,UTF-8),
_newlog =文本(文本+ R+日志:日志;
fs.writefile(global.logfile,_newlog,功能(ERR){)
如果(错误)抛出错误;
});
}
};
出口工具=工具;



。/ global.js是当地的一个全局变量文件,和我们现在使用的js代码测试使用NodeUnit上面的代码:


VaR工具=新要求('。 /工具。JS的);
this.testforutils = { {
testgetguid:功能(测试){
VaR的GUID = utils.utils.getguid();
Test.ok(!!!GUID,'getguid不应该是空的。);
Test.done();
},
testwritelog:功能(测试){
var标志= false;
Utils.utils.writeLog(测试信息);
标志=真;
test.ok(国旗,'writelog);
Test.done();
},
teststartwithwords:功能(测试){
变量名=ad_123 ;
test.ok(utils.utils.startwith(名称,ad_ ),从方法应该是好的);
Test.done();
}
};



test.ok也是通常的说法,nodeunit的单元测试程序,你也可以使用节点检查调试