javascript:
==:不严谨的比较,不会区分类型如:8=="8",得出的结果是true,===严谨的比较,既比较值,又比较
类型。如9===“9”,返回的是false.
一》使用assert模块进行测试:
使用步骤:
1)在脚本中请求assert模块。
var assert=require("assert");
2)使用strictEqual()方法来区分。
如:assert.strictEqual("a","a");
如果相等,则什么也不输出。反之则输入内容。
3)使用strictEqual();定制消息:
如:assert.strictEqual("b","c",'不相等');
结果如下:
D:\nodejs\test>node app.js
assert.js:92
throw new assert.AssertionError({
^
AssertionError: 锟斤拷锟斤拷锟斤拷
at Object.<anonymous> (D:\nodejs\test\app.js:13:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
D:\nodejs\test>node app.js
assert.js:92
throw new assert.AssertionError({
^
AssertionError: 涓嶇浉绛?
at Object.<anonymous> (D:\nodejs\test\app.js:13:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
可以看出定制的消息已经打印出来,但是出现乱码,而且不同的方式,乱码的字体不同。
这是什么原因呢,我在百度上查了也没查出来。
二》使用nodeunit测试
1)使用全局标志全局的安装到计算机上。
npm -g install nodeunit
var fs = require('fs');
exports.asyncTest = function(test){
fs.stat('test.txt', function(err, stats) {
test.expect(2);
test.strictEqual(err, null);
test.notStrictEqual(stats.size, 0);
test.done();
})
};
D:\nodejs\test>nodeunit app.js
app.js
FAILURES: Undone tests (or their setups/teardowns):
- asyncTest
To fix this, make sure all tests call test.done()
D:\nodejs\test>