• Node.js Video Tutorials

Node.js - assert.equal() 函式



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

Node.js assert.equal() 函式將使用 == 運算子測試其兩個輸入引數的淺比較。

當兩個引數相同時,函式不會丟擲 AssertionError。如果兩個引數不相等,則它會向輸出丟擲一個 AssertionError。此函式是 assert.strictEqaul() 函式的別名。

語法

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

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

引數

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

  • actual − (必需)此引數中傳遞的值將被評估。該值可以是任何型別。

  • expected − (必需)此引數中傳遞的值將與實際值進行比較。該值可以是任何型別。

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

返回值

如果 actualexpected 不匹配,則此函式將在終端返回 AssertionError

注意 - 兩側(actual & expected)的 NaN 被認為是相同的,因為它被特殊處理。

示例

在下面的示例中,我們將兩個相同的整數作為 actionexpected 引數傳遞給 Node.js assert.equal() 函式。

const assert = require('assert');
var num1 = 99;
var num2 = 99;
assert.equal(num1, num2, 'Both numbers are same');

輸出

由於 action 和 expected 相同,因此在執行程式碼後,函式不會向輸出丟擲任何 AssertionError

// Returns nothing

示例

在下面的示例中,我們將兩個相同的字串傳遞給 Node.js assert.equal() 函式的 actionexpected 引數。

const assert = require('assert');
var txt1 = 'Tutorialspoint';
var txt2 = 'Tutorialspoint';
assert.equal(txt1, txt2, 'Both messages are same');

輸出

當我們編譯並執行程式碼時,函式不會向輸出丟擲 AssertionError,因為 action 和 expected 相同。

// Returns nothing

示例

在下面的示例中,我們將兩個輸入傳遞給 Node.js assert.equal() 函式,一個整數傳遞給 actual 引數,一個字串傳遞給 expected 引數。

const assert = require('assert');
var num1 = 7;
var txt1 = '7';
assert.equal(num1, txt1, 'Both values are same');

輸出

當我們編譯並執行程式碼時,函式不會向輸出丟擲 AssertionError。這是正確的,因為 7 == '7'

// Returns nothing

示例

在下面的示例中,我們將兩個不同的整數傳遞給函式的 actionexpected 引數。

const assert = require('assert');
assert.equal(45, 66, 'Both numbers are not identical');

輸出

當我們編譯並執行程式碼時,函式將丟擲 AssertionError 以及訊息到輸出。這是錯誤的,因為 45 !== 66

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both numbers are not identical
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/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 引數。

const assert = require('assert');
assert.equal(45, 'Hello', 'Both values are not identical');

輸出

當我們編譯並執行程式碼時,函式不會向輸出丟擲 AssertionError。這是錯誤的,因為這兩個值是不同的 45 !== 'Hello'

assert.js:79
   throw new AssertionError(obj);
   ^
   
AssertionError [ERR_ASSERTION]: Both values are not identical
   at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/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)
nodejs_assert_module.htm
廣告