Node.js速学之-测试

参考文献:[1][EN]Georgo Ornbo.傅强.陈宗斌. Node.js入门经典[M]. 北京:人民邮电出版社.2013.4-1

测试是可重复的代码片段,可对应用程序是否以期望方式进行工作作出断言(assert).
一、Assert(断言模块)
方法:
1、assert.equal();
2、assert.strictEqual()//使用Javascript运算发比较;
测试一:

var assert = require('assert');
assert.strictEqual('hello','hello');

控制台:空
测试二:

var assert = require('assert');
assert.strictEqual('hello','hell0');

控制台:

assert.js:90
  throw new assert.AssertionError({
  ^
AssertionError: 'hello' === 'hell0'
    at Object. (c:\Users\Claves\WebstormProjects\Nodejs1\start.js:2:8)
    at Module._compile (module.js:399:26)
    at Object.Module._extensions..js (module.js:406:10)
    at Module.load (module.js:345:32)
    at Function.Module._load (module.js:302:12)
    at Function.Module.runMain (module.js:431:10)
    at startup (node.js:141:18)
    at node.js:977:3

二、第三方工具Nodeunit
Nodeunit构建于assert模块之上,添加了建立和拆卸测试的能力、异步测试能力以及模拟(mock)和桩(stub)的功能
三、行为驱动开发BDD
Node.js推崇使用BDD(行为驱动开发),而非TDD(测试驱动开发),而BDD应用程序与外来交互为基础。