Assert : Assertion testing

更新时间:
2024-05-13
下载文档

Assert : Assertion testing

The assert module provides a simple set of assertion tests that can be used to test invariants.

User can use the following code to import the assert module.

var assert = require('assert');

Support

The following shows assert module APIs available for each permissions.

 User ModePrivilege Mode
assert.AssertionError
assert
assert.ok
assert.fail
assert.equal
assert.notEqual
assert.strictEqual
assert.notStrictEqual
assert.throws
assert.doesNotThrow

assert.AssertionError Class

new assert.AssertionError(options)

  • options {Object} Assertion error options.
  • Returns: {Object} Returns assert.AssertionError object.

options options can have the following attributes:

  • message {String} If provided, the error message is set to this value.
  • actual {Any} The actual property on the error instance.
  • expected {Any} The expected property on the error instance.
  • operator {String} The operator property on the error instance.

assert.AssertionError is a subclass of Error that indicates the failure of an assertion.

All instances contain the built-in Error properties (message and name) and:

  • actual {Any} Set to the actual argument for methods such as assert.strictEqual().
  • expected {Any} Set to the expected value for methods such as assert.strictEqual().
  • generatedMessage {boolean} Indicates if the message was auto-generated (true) or not.
  • operator {String} Set to the passed in operator value.

Example

const assert = require('assert');

// Generate an AssertionError to compare the error message later:
const { message } = new assert.AssertionError({
  actual: 1,
  expected: 2,
  operator: 'strictEqual'
});

// Verify error output:
try {
  assert.strictEqual(1, 2);
} catch (err) {
  assert(err instanceof assert.AssertionError);
  assert.strictEqual(err.message, message);
  assert.strictEqual(err.name, 'AssertionError');
  assert.strictEqual(err.actual, 1);
  assert.strictEqual(err.expected, 2);
  assert.strictEqual(err.operator, 'strictEqual');
  assert.strictEqual(err.generatedMessage, true);
}

assert Functions

assert(value[, message])

  • value {Any} The input that is checked for being truthy.
  • message {String} | {Error} Error message.

An alias of assert.ok().

assert.ok(value[, message])

  • value {Any} The input that is checked for being truthy.
  • message {String} | {Error} Error message.

Checks if the value is truthy. If it is not, throws an AssertionError, with the given optional message.

Example

assert.ok(1);
// OK

assert.ok(true);
// OK

assert.ok(false);
// throws "AssertionError: false == true"

assert.ok(0);
// throws "AssertionError: 0 == true"

assert.ok(false, "it's false");
// throws "AssertionError: it's false"

assert.fail(actual, expected[, message[, operator]])

  • actual {Any} The actual value.
  • expected {Any} The expected value.
  • message {Any} Message to be displayed.
  • operator *{String}() The operator.

Throws an AssertionError exception with the given message.

Example

assert.fail(1, 2, undefined, '>');
// AssertionError: 1 > 2

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

  • actual {Any} The actual value.
  • expected {Any} The expected value.
  • message {Any} Message to be displayed.

Tests if actual == expected is evaluated to true. Otherwise throws an exception with the given optional message.

Example

assert.equal(1, 1);
assert.equal(1, '1');

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

  • actual {Any} The actual value.
  • expected {Any} The expected value.
  • message {Any} Message to be displayed.

Tests if actual != expected is evaluated to true. Otherwise throws an exception with the given optional message.

Example

assert.notEqual(1, 2);

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

  • actual {Any} The actual value.
  • expected {Any} The expected value.
  • message {Any} Message to be displayed.

Tests if actual === expected is evaluated to true. Otherwise throws an exception with the given optional message.

Example

assert.strictEqual(1, 1);
// OK

assert.strictEqual(1, 2);
// AssertionError: 1 === 2

assert.strictEqual(1, '1');
// AssertionError: 1 === '1'

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

  • actual {Any} The actual value.
  • expected {Any} The expected value.
  • message {Any} Message to be displayed.

Tests if actual !== expected is evaluated to true. Otherwise throws an exception with the given optional message.

Example

assert.notStrictEqual(1, 2);
// OK

assert.notStrictEqual(1, 1);
// AssertionError: 1 !== 1

assert.notStrictEqual(1, '1');
// OK

assert.throws(block[, expected, message])

  • block {Function} The function that throws an error.
  • expected {Function} The expected error type.
  • message {Any} Message to be displayed.

Tests if the given block throws an expected error. Otherwise throws an exception with the given optional message.

Example

assert.throws(function() {
  assert.equal(1, 2);
}, assert.AssertionError);
// OK

assert.throws(function() {
  assert.equal(1, 1);
}, assert.AssertionError);
// Uncaught error: Missing exception

assert.throws(function() {
  assert.equal(1, 2);
}, TypeError);
// AssertionError

assert.doesNotThrow(block[, message])

  • block {Function}
  • message {Any} Message to be displayed.

Tests if the given block does not throw any exception. Otherwise throws an exception with the given optional message.

Example

assert.doesNotThrow(function() {
  assert(1);
});
// OK

assert.doesNotThrow(function() {
  assert(0);
});
// throws "AssertionError: Got unwanted exception."
文档内容是否对您有所帮助?
有帮助
没帮助