知识共享许可协议
本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可。

Node.js v0.10.18 手册 & 文档


断言 (assert)#

稳定度: 5 - 已锁定

This module is used for writing unit tests for your applications, you can access it with require('assert').

此模块主要用于对您的程序进行单元测试,要使用它,请 require('assert')

assert.fail(actual, expected, message, operator)#

Throws an exception that displays the values for actual and expected separated by the provided operator.

抛出异常:显示为被 operator (分隔符)所分隔的 actual (实际值)和 expected (期望值)。

assert(value, message), assert.ok(value, [message])#

Tests if value is truthy, it is equivalent to assert.equal(true, !!value, message);

测试结果是否为真(true),相当于 assert.equal(true, !!value, message);

assert.equal(actual, expected, [message])#

Tests shallow, coercive equality with the equal comparison operator ( == ).

浅测试, 强制相等就像使用相等操作符( == ).

assert.notEqual(actual, expected, [message])#

Tests shallow, coercive non-equality with the not equal comparison operator ( != ).

Tests shallow, coercive non-equality with the not equal comparison operator ( != ).

assert.deepEqual(actual, expected, [message])#

Tests for deep equality.

用于深度匹配测试。

assert.notDeepEqual(actual, expected, [message])#

Tests for any deep inequality.

用于深度非匹配测试。

assert.strictEqual(actual, expected, [message])#

Tests strict equality, as determined by the strict equality operator ( === )

用于严格相等匹配测试,由(===)的结果决定

assert.notStrictEqual(actual, expected, [message])#

Tests strict non-equality, as determined by the strict not equal operator ( !== )

严格不相等测试, 强制不相等就像使用严格不相等操作符( !== ).

assert.throws(block, [error], [message])#

Expects block to throw an error. error can be constructor, regexp or validation function.

输出一个错误的 blockerror 可以是构造函数,正则或者验证函数

Validate instanceof using constructor:

使用验证实例的构造函数

assert.throws(
  function() {
    throw new Error("错误值");
  },
  Error
);

Validate error message using RegExp:

用正则表达式验证错误消息。

assert.throws(
  function() {
    throw new Error("错误值");
  },
  /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"
);

assert.doesNotThrow(block, [message])#

Expects block not to throw an error, see assert.throws for details.

预期 block 不会抛出错误,详见 assert.throws。

assert.ifError(value)#

Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.

测试值是否不为 false,当为 true 时抛出。常用于回调中第一个 error 参数的检查。