node.js - 与断言(assert.ok)相比,assert.equal有什么优势吗?

标签 node.js assert

在这个问题中我引用了assert包含在 Node.js 核心中的模块。

据我所知,以下两个断言几乎相同:

assert.equal(typeof path, "string", "argument 'path' must be a string");
assert(typeof path === "string", "argument 'path' must be a string");

失败时,两种变体都会报告相同的消息:

AssertionError: argument 'path' must be a string

在这种情况下,前者相对于后者有什么显着的优势吗?

最佳答案

好吧,根据测试运行器框架,assert.equal 可能会为您提供更具描述性的错误消息。例如,在本例中:

assert.equal(typeof path, "string");
assert(typeof path === "string");

第一个语句会给您一条消息:

actual: number
expected: string

它已经告诉您测试用例失败,因为typeof path是一个number。 后者只会打印如下内容:

AssertionError: false == true

另请注意,如果您想检查严格相等 (===),则应使用 assert.strictEqual 而不是 assert.equal

关于node.js - 与断言(assert.ok)相比,assert.equal有什么优势吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34440927/

相关文章:

parallel-processing - MPI 程序的断言函数

node.js - 安装软件包时卡住了。 npm 错误! notarget 找不到 sockjs-client@0.0.0-unreleasable 的匹配版本

.net - 如何使用 "proper"调用堆栈创建自定义 MSTest 断言方法

c++ - 在什么情况下我们需要包含<cassert>?

Node.js - 要求未定义异常

c++ - 在 C++ 类中折叠断言?

python - 是否可以在脚本中设置 python -O(优化)标志?

Javascript - 从 If/Else block 外部调用变量返回未定义

node.js - 警告 : occur node apn emitter. setMaxListeners()

javascript - 在 Node 中生成以毫秒为单位的时间戳