Instal melalui NPM:
NPM Instal NodeUnit -G
Mendukung baris perintah, browser berjalan. Berbagai pernyataan. Ekspor ekspor modular untuk metode di node.js. Jika Anda mengekspor modul. Exports untuk objek, modul adalah dasar untuk pengujian unit. Lihat kode Node.js berikut:
var fs = membutuhkan ('fs'), global = membutuhkan ('./ 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. true;},/ * Hasilkan Guid */getGuid: function () {var guid = ""; for (var i = 1; i <= 32; i ++) {var n = math.floor (math.random () * 16.0) .toString (16); Guid+n;} return guid;},// * Tambah Log Informasi (16); GUID+= N;} Return Guid;},// * Tambah Log Information (16); GUID+= N;} Return Guid;},// * Tambah Log Information (16); GUID+= N;} Return GUID;},//TAMBAH INFORMASI ADD/LOG:! fs.readfilesync (global.logfile, "UTF-8"), _ newlog = teks? (Teks + "/r/n" + log): log; fs.writefile (global.logfile, _newlog, function (err) {if (err) throw err;});}}; exports.utils = utils;./global.js adalah file variabel global lokal. Sekarang kami menggunakan nodeunit untuk menguji kode di atas:
var utils = new membutuhkan ('./ utils.js'); this.testforutils = {'testgetguid': function (test) {var guid = utils.utils.getguid (); test.ok (!! guDguid tidak boleh null. '); test.done ();},' uji 'function': test.done ();}, 'test' function ': test.done (); flag = false; utils.utils.writelog ("pesan tes"); flag = true; test.ok (flag, 'writelog'); test.done ();}, 'teststartwithwords': function (test) {var name = "ad_123"; test.ok (utils.utils.startwith (name, "ad _"; ok "); test.done ();}};test.ok juga merupakan pernyataan yang biasanya kami katakan. Untuk program uji unit nodeunit, Anda juga dapat menggunakan node-inspector untuk men-debug