在这个问题中我引用了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/