
- 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.deepEqual() 函式
NodeJS assert.deepEqual() 函式將測試其兩個引數actual和expected的深度相等性。當兩個引數相同時,函式不會丟擲AssertionError。如果兩個引數不相等,則會向輸出丟擲AssertionError。此函式是assert.deepStrictEqual()函式的別名。
Node.js assert.deepEqual() 函式是 Node.js assert 模組的內建函式。
語法
以下是Node.js assert.deepEqual() 函式的語法:
assert.deepEqual(actual, expected[, message]);
引數
此函式接受三個引數。具體描述如下。
actual − (必填) 此引數中傳遞的值將被評估。值可以是任何型別。
expected − (必填) 此引數中傳遞的值將與actual值進行比較。值可以是任何型別。
message − (可選) 可以將字串或 Error 型別作為輸入傳遞到此引數。
返回值
如果actual和expected不匹配,此函式將在終端返回AssertionError。
示例
在下面的示例中,我們執行兩個數字的加法和乘法並將結果儲存起來。然後,我們將這兩個結果作為actual和expected傳遞給 Node.js assert.deepEqual() 函式,並將文字傳遞給message引數。
const assert = require('assert'); var x = 10, y = 5, z = 6; var add = x + z; var mul = y * x; assert.deepEqual(add, mul, 'Both the values are not similar');
輸出
當我們編譯並執行程式碼時,由於actual和expected不匹配,該函式將連同message一起將AssertionError輸出。
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Both the values are not similar at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:7: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)
示例
在下面的示例中,我們建立了兩個具有某些屬性的物件。然後,我們將這兩個物件作為actual和expected傳遞給 Node.js assert.deepEqual() 函式,並將文字傳遞給message引數。
const assert = require('assert'); var obj1 = {'name': 'Jay', 'age': 21}; var obj2 = {'name': 'Vikram', 'age': 51}; assert.deepEqual(obj1, obj2, 'Both the objects are not identical');
輸出
當我們編譯並執行程式碼時,由於兩個actual和expected並不完全相同,該函式將連同message一起將AssertionError輸出。
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Both the objects are not identical at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:6: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)
示例
在下面的示例中,我們建立了兩個整數陣列,並且這兩個陣列是相同的。然後,我們將這兩個陣列作為actual和expected傳遞給 Node.js assert.deepEqual() 函式,並將文字傳遞給message引數。
const assert = require('assert'); var array1 = [1, 2, 3, 4, 5]; var array2 = [1, 2, 3, 4, 5]; assert.deepEqual(array1, array2, 'Both the arrays identical');
輸出
當我們編譯並執行程式碼時,由於actual和expected完全相同,該函式不會向輸出丟擲任何AssertionError。相反,函式不會向輸出返回任何內容。
// Returns nothing