• Node.js Video Tutorials

Node.js - assert.throws() 函式



assert 模組提供了一組斷言函式,用於驗證不變式。Node.js 的 assert.throws() 函式是 Node.js assert 模組的內建函式。

Node.js assert.throws() 函式期望傳入的函式丟擲一個錯誤。該函式將接收一個作為輸入的函式,該函式預計會丟擲錯誤。

除了此引數外,此函式還接受兩個可選引數:一個包含有關錯誤性質的資訊的 Error 物件,以及一個用於除錯的訊息,以及與錯誤相關的其他資料。

此方法允許開發人員根據需要輕鬆建立具有特定訊息和上下文的自定義錯誤。throws() 方法還提供堆疊跟蹤資訊,使除錯程式碼錯誤變得更容易。

語法

以下是 Node.js assert.throws() 函式 的語法:

assert.throws(fn[, error][, message]);

引數

此函式接受三個引數。下面描述了這些引數。

  • fn − (必填) 此引數儲存一個函式 fn,並期望該函式丟擲一個錯誤。

  • error − (可選) 此引數可以是 RegExp驗證函式物件Error(錯誤例項)。

  • message − (可選) 可以將字串或 Error 型別作為輸入傳遞給此引數。

返回值

如果函式 fn 丟擲錯誤,則 assert.throws() 函式將返回一個物件型別的 AssertionError。

示例

在示例中,我們將一個包含錯誤訊息的函式傳遞給第一個引數。我們將 RegExp 傳遞給第二個引數,該引數應與函式內部的錯誤訊息匹配。

const assert = require('assert');
function func1() {
   assert.throws(() => { func2(); }, /TypeError: Error happening.../);
}

function func2() {
   throw new TypeError('Error ...');
}

func1();

輸出

由於 RegExp 與訊息不匹配,因此我們將測試的函式將向輸出丟擲一個帶有特定訊息的錯誤。

assert.js:578
   throw actual;
   ^
   
TypeError: Error ...
   at func2 (/home/cg/root/639c3f570123e/main.js:8:9)
   at assert.throws (/home/cg/root/639c3f570123e/main.js:4:25)
   at getActual (assert.js:497:5)
   at Function.throws (assert.js:608:24)
   at func1 (/home/cg/root/639c3f570123e/main.js:4:10)
   at Object.<anonymous> (/home/cg/root/639c3f570123e/main.js:11:1)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)at tryModuleLoad (internal/modules/cjs/loader.js:551:12)

示例

在以下示例中,我們將一個包含錯誤訊息的函式傳遞給第一個引數。然後我們將 RegExp 傳遞給第二個引數,該引數應與函式內部的錯誤訊息匹配。

const assert = require('assert');
function func1() {
   assert.throws(() => { func2(); }, /TypeError: Lannisters pay their debts.../);
}

function func2() {
   throw new TypeError('Lannisters pay their debts...');
}

func1();

輸出

由於 RegExp 與訊息匹配,因此我們將測試的函式不會向輸出丟擲錯誤。

// Returns nothing

注意 − 函式的第二個引數 error 不能是字串。如果將字串作為 error 傳遞,則假定省略了 error,並且該字串將用於函式的 message 引數。如果使用的訊息與丟擲的錯誤訊息相同,則會生成 ERR AMBIGUOUS ARGUMENT 錯誤。

如果將字串用作第二個引數,請仔細檢視以下示例:

示例

在以下示例中:

  • 我們建立了三個包含錯誤訊息的函式。

  • 然後我們將特定函式作為第一個引數傳遞給 assert.throws() 函式,並將字串作為第二個引數傳遞。

const assert = require('assert');
function func1() {
   throw new Error('one');
};

function func2() {
   throw new Error('two');
};

function funcNotThrwoing() {};

assert.throws(() => {func2(); }, 'two');

輸出

由於不清楚使用者是否打算將字串與錯誤訊息匹配。該函式將丟擲一個 `ERR_AMBIGUOUS_ARGUMENT` 錯誤。

assert.js:547
   throw new ERR_AMBIGUOUS_ARGUMENT(
   ^
   
TypeError [ERR_AMBIGUOUS_ARGUMENT]: The "error/message" argument is ambiguous. The error message "two" is identical to the message.
   at expectsError (assert.js:547:15)
   at Function.throws (assert.js:608:3)
   at Object.<anonymous> (/home/cg/root/639c3f570123e/main.js:13:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)

示例

在以下示例中:

  • 我們建立了三個包含錯誤訊息的函式。

  • 然後我們將特定函式作為第一個引數傳遞給 assert.throws() 函式,並將字串作為第二個引數傳遞。

const assert = require('assert');
function func1() {
   throw new Error('one');
};

function func2() {
   throw new Error('two');
};

function funcNotThrwoing() {};

assert.throws(() => {func2(); }, 'one');

輸出

輸入函式不會丟擲錯誤,因為字串與輸入函式中的錯誤訊息不匹配。

// Returns nothing

示例

在此示例中,目的是讓字串與錯誤訊息匹配。

const assert = require('assert');
function func1() {
   throw new Error('one');
};

function func2() {
   throw new Error('two');
};

function funcNotThrwoing() {};

assert.throws(() => {func1(); }, /one$/);

輸出

該函式不會丟擲任何錯誤,因為 RegExp 與輸入函式中存在的錯誤訊息匹配。

// Returns nothing

示例

在此示例中,目的是讓字串與錯誤訊息匹配。

const assert = require('assert');
function func1() {
   throw new Error('one');
};

function func2() {
   throw new Error('two');
};

function funcNotThrwoing() {};
assert.throws(() => {func2(); }, /one$/);

輸出

該函式丟擲錯誤,因為 RegExp 與輸入函式中存在的錯誤訊息不匹配。

node main.js
assert.js:578
   throw actual;
   ^
   
Error: two
   at func2 (/home/cg/root/639c44d7022a4/main.js:8:9)
   at assert.throws (/home/cg/root/639c44d7022a4/main.js:13:22)
   at getActual (assert.js:497:5)
   at Function.throws (assert.js:608:24)
   at Object.<anonymous> (/home/cg/root/639c44d7022a4/main.js:13:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)

示例

由於輸入函式沒有丟擲任何錯誤,因為它沒有任何錯誤訊息。

const assert = require('assert');
function func1() {
   throw new Error('one');
};

function func2() {
   throw new Error('two');
};

function funcNotThrwoing() {};

assert.throws(() => {funcNotThrwoing(); }, /one$/);

輸出

正如我們在下面的輸出中看到的,該函式丟擲一個帶有“缺少預期異常”訊息的錯誤。

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Missing expected exception.
   at Object.<anonymous> (/home/cg/root/639c44d7022a4/main.js:13:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
nodejs_assert_module.htm
廣告

© . All rights reserved.