Assert.fail(node.js):运算符参数是什么意思?

Node.jsunit testing模块有基本的assertion assert.fail:

assert.fail(actual, expected, message, operator) 

operator是什么意思? 我真的很陌生,unit testing…

什么文档说: operator的价值被用来提供一个错误信息时分开的actual值和expected值。 这在断言模块的 Node.js 文档中有描述。

但是,如果您在交互式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) 

当你看一下101-109行的assert模块的实现时,

 function fail(actual, expected, message, operator, stackStartFunction) { throw new assert.AssertionError({ message: message, actual: actual, expected: expected, operator: operator, stackStartFunction: stackStartFunction }); } 

因此,更好的描述可能是它不会自动在消息中使用,但是如果您捕获exception并自己创build适当的消息,则可以使用它。 因此,如果您要创build自己的testing框架,则此参数可能很有用。

如果你省略了message参数,你可以强制Node.js使用该参数,例如通过显式地传递undefined

 > assert.fail(23, 42, undefined, '###') AssertionError: 23 ### 42 [...]