Node.js assert.notStrictEqual()函数
assert 模块提供了一组用于验证不变量的断言函数。 assert.notStrictEqual()函数测试实际参数和预期参数之间的严格不等式。如果条件为真,则不会产生输出,否则会引发断言错误。句法:
assert.notStrictEqual(actual, expected[, message])
参数:该函数有三个参数,实参为任意类型,预期参数为任意类型,消息参数可以为字符串或错误类型。
返回值:此函数返回对象类型的断言错误。
安装断言模块:
- 您可以访问安装断言模块的链接。您可以使用此命令安装此软件包。
npm install assert
- 注意:安装是一个可选步骤,因为它是内置的 Node.js 模块。
- 安装 assert 模块后,您可以使用命令在命令提示符下检查您的断言版本。
npm version assert
- 之后,您可以创建一个文件夹并添加一个文件,例如 index.js,如下所示。
示例 1:文件名:index.js
javascript
// Requiring the module
const assert = require('assert').strict;
var a = 2;
var b = 2;
// Function call
try {
assert.notStrictEqual(a, b)
} catch(error) {
console.log("Error: ", error)
}
javascript
// Requiring the module
const assert = require('assert').strict;
var a = 4;
var b = "Four";
// Function call
try {
assert.notStrictEqual(a, b)
console.log("No Error Occured")
} catch(error) {
console.log("Error: ", error)
}
a = 4;
b = 4;
// Function call
try {
assert.notStrictEqual(a, b)
} catch(error) {
console.log("Error Occured: ", error)
}
运行程序的步骤:
- 项目结构将如下所示:
- 使用以下命令运行index.js文件:
node index.js
- 输出:
Error: AssertionError [ERR_ASSERTION]: Expected “actual” to be strictly unequal to: 2
at Object. (C:\Users\Lenovo\Downloads\index.js:9:12)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
generatedMessage: true,
code: ‘ERR_ASSERTION’,
actual: 2,
expected: 2,
operator: ‘notStrictEqual’
}
示例 2:文件名:index.js
javascript
// Requiring the module
const assert = require('assert').strict;
var a = 4;
var b = "Four";
// Function call
try {
assert.notStrictEqual(a, b)
console.log("No Error Occured")
} catch(error) {
console.log("Error: ", error)
}
a = 4;
b = 4;
// Function call
try {
assert.notStrictEqual(a, b)
} catch(error) {
console.log("Error Occured: ", error)
}
运行程序的步骤:
- 项目结构将如下所示:
- 使用以下命令运行index.js文件:
node index.js
- 输出:
No Error Occured
Error Occured: AssertionError [ERR_ASSERTION]: Expected “actual” to be strictly
unequal to: 4
at Object. (C:\Users\Lenovo\Downloads\index.js:20:12)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
generatedMessage: true,
code: ‘ERR_ASSERTION’,
actual: 4,
expected: 4,
operator: ‘notStrictEqual’
}
参考: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_notstrictequal_actual_expected_message