- 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.strictEqual() 函式
assert 模組提供了一組斷言函式,用於驗證不變式。Node.js 的 assert.strictEqual() 函式是 Node.js assert 模組的內建函式。
Node.js 的 strictEqual() 函式用於確定兩個值是否相等,它使用比 == 運算子更嚴格的比較。此函式比較兩個變數或物件的 資料型別和值,只有當它們完全相同 時才返回 true。此外,將 NaN(非數字)與任何其他值(包括自身)進行比較都將返回 false。
此函式將測試其兩個引數 actual 和 expected 的相等性。當兩個引數相同時,函式不會丟擲 AssertionError。如果兩個引數不相等,則它會將 AssertionError 丟擲到輸出。此函式是 Node.js assert.deepStrictEqual() 函式的別名。
語法
以下是 Node.js assert.strictEqual() 函式 的語法:
assert.strictEqual(actual, expected[, message]);
引數
此函式接受三個引數。如下所述。
actual − (必需)此引數中傳遞的值將被評估。值可以是任何型別。
expected − (必需)此引數中傳遞的值將與 actual 值進行比較。值可以是任何型別。
message − (可選)可以將字串或 Error 型別作為輸入傳遞到此引數。
返回值
如果 actual 和 expected 不匹配,此函式將在終端返回 AssertionError。
示例
在以下示例中,我們將兩個相同的整數傳遞給 Node.js assert.strictEqual() 函式的 actual 和 expected 引數。
const assert = require('assert');
var num1 = 45;
var num2 = 45;
assert.strictEqual(num1, num2, 'Both the values are same');
輸出
如果我們編譯並執行程式碼,則不會將任何 AssertionError 丟擲到輸出。這是正確的,因為 45 == 45。
// Returns nothing
示例
在以下示例中,我們將兩個不同的整數傳遞給函式的 actual 和 expected 引數。
const assert = require('assert');
var int1 = 34;
var int2 = 10;
assert.strictEqual(int1, int2, 'Both the values are not same');
輸出
如果我們編譯並執行程式碼,它將向輸出丟擲 AssertionError 以及 message 引數中的文字。這是錯誤的,因為 34 !== 10。
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Both the values are not same at Object.<anonymous> (/home/cg/root/639c3f570123e/main.js:5: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 引數。
const assert = require('assert');
var text1 = 'Welcome to India';
var text2 = 'Welcome to India';
assert.strictEqual(text1, text2, 'Both the strings are identical');
輸出
如果我們編譯並執行程式碼,則不會將任何 AssertionError 丟擲到輸出。這是正確的,因為 text1 == text2。
// Returns nothing
示例
在以下示例中,我們傳遞兩個字串值並檢查它們的型別。
const assert = require('assert');
var text1 = 'Rise';
var text2 = 'Rule';
assert.strictEqual(typeof text1 === 'string', typeof text2 === 'number', 'Both the strings are not identical');
輸出
如果我們編譯並執行程式碼,它將丟擲一個 AssertionError 以及message中的文字到輸出。這是錯誤的,因為'text2' == 'number'。
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Both the strings are not identical at Object.<anonymous> (/home/cg/root/639c3f570123e/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)
示例
在下面的示例中,我們傳遞兩個不同的字串值,並且沒有向message引數傳遞任何文字。
const assert = require('assert');
assert.strictEqual('RRR', 'ZZZ');
輸出
如果我們編譯並執行程式碼,它將向輸出丟擲 AssertionError,並且函式將分配預設訊息。
assert.js:79 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B: + expected - actual - 'RRR' + 'ZZZ' at Object.<anonymous> (/home/cg/root/639c3f570123e/main.js:3: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和message引數傳遞任何輸入值。
const assert = require('assert');
assert.strictEqual();
輸出
// Returns Nothing
注意 - 有時線上編譯器可能無法給出預期的結果,因此我們會在本地執行上述程式碼。
如果我們編譯並執行程式碼,該函式將丟擲 AssertionError,並顯示預設訊息“The 'actual' and 'expected' arguments must be specified”。
node:assert:568
throw new ERR_MISSING_ARGS('actual', 'expected');
^
TypeError [ERR_MISSING_ARGS]: The "actual" and "expected" arguments must be specified
at new NodeError (node:internal/errors:387:5)
at Function.strictEqual (node:assert:568:11)
at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:3:8)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'ERR_MISSING_ARGS'
}
