Nodejs学习笔记——Assert(断言)
- Assert - a:actual e:expected m:message o:operator v:value b:block
- assert.fail(a, e, m, o)
- assert(v, m), assert.ok(v, [m])
- assert.equal(a, e, [m])
- assert.notEqual(a, e, [m])
- assert.deepEqual(a, e, [m])
- assert.notDeepEqual(a, e, [m])
- assert.strictEqual(a, e, [m])
- assert.notStrictEqual(a, e, [m])
- assert.throws(b, [error], [m])
- assert.doesNotThrow(b, [m])
- assert.ifError(v)
Throws an exception that displays the values for actual and expected separated by the provided operator.
Note: Always throws an exception and display the message which will be "actual operator expected" if it's omited.
e.g assert.fail(1,1,"Always throws this message","=") and assert.fail(1,1,"","+")
Tests if value is truthy, it is equivalent to assert.equal(true, !!value, message);
Note: If the value isn't a truth-value, it throws and exception and display the message which will be "value == true" if it's omited.
e.g assert(0) and assert.ok(0,"Throws me")
Tests shallow, coercive equality with the equal comparison operator ( == ).
e.g assert.equal(1,2,"They are not equal") and assert.equal(1,1,"would not show me")
Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
Note: 综上,是对比结果为false时抛出异常。
Tests for deep equality.
Note: 采用以下步骤比较,只要一个步骤不匹配就抛出异常:
- 采用
===比较; - 比较他们是否是Buffers,如果是则比较长度,接下来每字节每字节比较;
- 用
==比较; - 最后如果参数是Object对象,则比较他们的属性长度和属性值
Tests for any deep inequality.
Tests strict equality, as determined by the strict equality operator ( === )
Tests strict non-equality, as determined by the strict not equal operator ( !== )
Expects block to throw an error. error can be constructor, RegExp or validation function.
Validate instanceof using constructor:
assert.throws(
function() {
throw new Error("Wrong value");
},
Error
);
Validate error message using RegExp:
assert.throws(
function() {
throw new Error("Wrong value");
},
/value/
);
Custom error validation:
assert.throws(
function() {
throw new Error("Wrong value");
},
function(err) {
if ( (err instanceof Error) && /value/.test(err) ) {
return true;
}
},
"unexpected error"
);
Expects block not to throw an error, see assert.throws for details.
Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.
Note: it throws error when the value is a truth-value!
Nodejs学习笔记——Assert(断言)的更多相关文章
- Nodejs学习笔记(四)——支持Mongodb
前言:回顾前面零零碎碎写的三篇挂着Nodejs学习笔记的文章,着实有点名不副实,当然,这篇可能还是要继续走着离主线越走越远的路子,从简短的介绍什么是Nodejs,到如何寻找一个可以调试的Nodejs ...
- Nodejs学习笔记(三)——一张图看懂Nodejs建站
前言:一条线,竖着放,如果做不到精进至深,那就旋转90°,至少也图个幅度宽广. 通俗解释上面的胡言乱语:还没学会爬,就学起走了?! 继上篇<Nodejs学习笔记(二)——Eclipse中运行调试 ...
- Nodejs学习笔记(二)——Eclipse中运行调试Nodejs
前篇<Nodejs学习笔记(一)——初识Nodejs>主要介绍了在搭建node环境过程中遇到的小问题以及搭建Eclipse开发Node环境的前提步骤.本篇主要介绍如何在Eclipse中运行 ...
- NodeJS学习笔记之Connect中间件模块(一)
NodeJS学习笔记之Connect中间件模块(一) http://www.jb51.net/article/60430.htm NodeJS学习笔记之Connect中间件模块(二) http://w ...
- Nodejs学习笔记(六)--- Node.js + Express 构建网站预备知识
目录 前言 新建express项目并自定义路由规则 如何提取页面中的公共部分? 如何提交表单并接收参数? GET 方式 POST 方式 如何字符串加密? 如何使用session? 如何使用cookie ...
- Nodejs学习笔记(十五)--- Node.js + Koa2 构建网站简单示例
目录 前言 搭建项目及其它准备工作 创建数据库 创建Koa2项目 安装项目其它需要包 清除冗余文件并重新规划项目目录 配置文件 规划示例路由,并新建相关文件 实现数据访问和业务逻辑相关方法 编写mys ...
- Nodejs学习笔记(十六)--- Pomelo介绍&入门
目录 前言&介绍 安装Pomelo 创建项目并启动 创建项目 项目结构说明 启动 测试连接 聊天服务器 新建gate和chat服务器 配置master.json 配置servers.json ...
- [转]Nodejs学习笔记(十五)--- Node.js + Koa2 构建网站简单示例
本文转自:https://www.cnblogs.com/zhongweiv/p/nodejs_koa2_webapp.html 目录 前言 搭建项目及其它准备工作 创建数据库 创建Koa2项目 安装 ...
- Nodejs学习笔记(十六)—Pomelo介绍&入门
前言&介绍 Pomelo:一个快速.可扩展.Node.js分布式游戏服务器框架 从三四年前接触Node.js开始就接触到了Pomelo,从Pomelo最初的版本到现在,总的来说网易出品还算不错 ...
随机推荐
- iOS中有关配置 Apache 服务器的详细步骤
配置 Apache 服务器 目的: 能够有一个测试的服务器,Apache 服务器是免费的! 为什么是 Apache 使用最广的 Web 服务器 Mac自带,只需要修改几个配置就可以,简单,快捷 有些特 ...
- (转)C# WinForm中 获得当前鼠标所在控件 或 将窗体中鼠标所在控件名显示在窗体标题上
原文地址:http://www.cnblogs.com/08shiyan/archive/2011/04/14/2015758.html /********************** * 课题:将窗 ...
- HDU 1021 - Fibonacci Again
找规律,分析让 F[N] 每一项对 3 取余的余数: 1,2,0, 2,2,1,0, 1,1,2,0, 2,2,1,0, 1,1,2,0, 2,2,1,0 ......... 显然循环了 #inclu ...
- MySQL取得当前时间的函数是什么 格式化日期的函数是什么
取得当前时间用 now() 就行.在数据库中格式化时间 用DATE_FORMA T(date, format) .根据格式串format 格式化日期或日期和时间值date,返回结果串. 可用DATE_ ...
- 转 jquery插件--241个jquery插件—jquery插件大全
241个jquery插件—jquery插件大全 jquery插件jqueryautocompleteajaxjavascriptcoldfusion jQuery由美国人John Resig创建,至今 ...
- PHP获取毫秒时间戳
我们知道,PHP中time()函数获取的时间戳,其单位是秒. 但是,前端JS获取的时间戳,单位是毫秒. 那么,在实际应用中,如何将JS和PHP的时间戳统一,即如何使用PHP获取毫秒时间戳呢,请看下例: ...
- Google机器学习教程心得(二)决策树与可视化
Visualizing a Decision Tree Google Machine Learning Recipes 2 官方中文博客 http://chinagdg.org/2016/03/mac ...
- Linux下安装Wireshark
Linux下安装Wireshark wireshark依赖于libpcap,所以如果系统中未安装libpcap,也要将其一并安装 一.下载源码 源码文件 wireshark-x.x.x.tar.gz ...
- openjpa框架入门_项目框架搭建(二)
Openjpa2.2+Mysql+Maven+Servlet+JSP 首先说明几点,让大家更清楚整体结构: 官方source code 下载:http://openjpa.apache.org/dow ...
- css案例学习之继承关系
代码 <html> <head> <title>继承关系</title> <style> body{ color:blue; /* 颜色 * ...