node.js应用程序的测试。



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>



















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值