Node.js 单元测试模块有基本的断言 assert.fail:
assert.fail(actual, expected, message, operator)
是什么operator
意思?我对单元测试真的很陌生...
Node.js 单元测试模块有基本的断言 assert.fail:
assert.fail(actual, expected, message, operator)
是什么operator
意思?我对单元测试真的很陌生...
文档内容: 的值operator
用于分隔 的值actual
和expected
提供错误消息时的值。这在 Node.js的 assert 模块文档中有所描述。
但是,如果您在交互式 shell 中尝试此操作,您会发现该参数似乎被忽略了:
> assert.fail(23, 42, 'Malfunction in test.', '###')
AssertionError: Malfunction in test.
at repl:1:9
at REPLServer.self.eval (repl.js:111:21)
at Interface.<anonymous> (repl.js:250:12)
at Interface.EventEmitter.emit (events.js:88:17)
at Interface._onLine (readline.js:199:10)
at Interface._line (readline.js:517:8)
at Interface._ttyWrite (readline.js:735:14)
at ReadStream.onkeypress (readline.js:98:10)
at ReadStream.EventEmitter.emit (events.js:115:20)
at emitKey (readline.js:1057:12)
当你看一下 assert 模块的实现时,这一切都是有道理的,第 101-109 行:
function fail(actual, expected, message, operator, stackStartFunction) {
throw new assert.AssertionError({
message: message,
actual: actual,
expected: expected,
operator: operator,
stackStartFunction: stackStartFunction
});
}
因此,更好的描述可能是它不会在消息中自动使用,但如果您捕获异常并自己创建适当的消息,则可以使用它。因此,如果您要创建自己的测试框架,此参数可能很有用。
如果您省略参数,您可以强制 Node.js 使用该message
参数,例如通过undefined
显式传递:
> assert.fail(23, 42, undefined, '###')
AssertionError: 23 ### 42
[...]