- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用工具模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
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)
