在这个问题中,我指的是Node.js 核心中包含的断言模块。
据我所知,以下两个断言几乎相同:
assert.equal(typeof path, "string", "argument 'path' must be a string");
assert(typeof path === "string", "argument 'path' must be a string");
Run Code Online (Sandbox Code Playgroud)
失败时,两种变体都会报告相同的消息:
AssertionError: argument 'path' must be a string
Run Code Online (Sandbox Code Playgroud)
在这种情况下,前者相对于后者有什么显着的优势吗?
好吧,根据测试运行器框架,assert.equal
可能会给您更具描述性的错误消息。例如,在这种情况下:
assert.equal(typeof path, "string");
assert(typeof path === "string");
Run Code Online (Sandbox Code Playgroud)
第一条语句会给您一条消息,大致如下:
actual: number
expected: string
Run Code Online (Sandbox Code Playgroud)
它已经告诉您测试用例失败,因为typeof path
是number
. 后者只会打印这样的内容:
AssertionError: false == true
Run Code Online (Sandbox Code Playgroud)
另请注意,如果您想检查严格相等 (===),则应assert.strictEqual
使用assert.equal
.
归档时间: |
|
查看次数: |
1267 次 |
最近记录: |