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

时间:2021-03-02 20:17:19

Node.js unit-testing module has basic assertion assert.fail:

Node.js单元测试模块有基本断言assert.fail:

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

What does operator mean? I'm really new to unit-testing...

运算符是什么意思?我对单元测试真的很陌生......

1 个解决方案

#1


9  

What the documentation says: The value of operator is being used to separate the values of actual and expected when providing an error message. This is described in Node.js' documentation for the assert module.

文档说明:在提供错误消息时,运算符的值用于分隔实际值和预期值。这在assert模块的Node.js文档中有所描述。

But, if you try this in the interactive shell you see that the parameter seems to be ignored:

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

It all makes sense when you take a look at the implementation of the assert module, lines 101-109:

当你看一下assert模块的实现时,这一切都是有意义的,第101-109行:

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

So, a better description might be that it is not used automatically in the message, but it can be used if you catch the exception and create an appropriate message yourself. In consequence, this parameter may be useful if you are going to create your own testing framework.

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

You can force Node.js to use that parameter if you omit the message parameter, e.g. by passing undefined explicitly:

如果省略message参数,则可以强制Node.js使用该参数。通过显式传递undefined:

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

#1


9  

What the documentation says: The value of operator is being used to separate the values of actual and expected when providing an error message. This is described in Node.js' documentation for the assert module.

文档说明:在提供错误消息时,运算符的值用于分隔实际值和预期值。这在assert模块的Node.js文档中有所描述。

But, if you try this in the interactive shell you see that the parameter seems to be ignored:

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

It all makes sense when you take a look at the implementation of the assert module, lines 101-109:

当你看一下assert模块的实现时,这一切都是有意义的,第101-109行:

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

So, a better description might be that it is not used automatically in the message, but it can be used if you catch the exception and create an appropriate message yourself. In consequence, this parameter may be useful if you are going to create your own testing framework.

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

You can force Node.js to use that parameter if you omit the message parameter, e.g. by passing undefined explicitly:

如果省略message参数,则可以强制Node.js使用该参数。通过显式传递undefined:

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