8

Node.js 単体テスト モジュールには、基本的なアサーション assert.fail があります。

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

とはoperatorどういう意味ですか? 私はユニットテストに本当に慣れていません...

4

1 に答える 1

9

ドキュメントの説明: の値は、エラー メッセージを提供するときにとoperatorの値を区切るために使用されています。これについては、アサート モジュールに関するNode.js のドキュメントで説明されています。actualexpected

ただし、対話型シェルでこれを試すと、パラメーターが無視されているように見えることがわかります。

> 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
  });
}

したがって、メッセージで自動的に使用されるわけではありませんが、例外をキャッチして適切なメッセージを自分で作成すれば使用できます。したがって、このパラメーターは、独自のテスト フレームワークを作成する場合に役立ちます。

パラメータを省略した場合、たとえば明示的messageに渡すことにより、 Node.js にそのパラメータを強制的に使用させることができます。undefined

> assert.fail(23, 42, undefined, '###')
AssertionError: 23 ### 42
[...]
于 2013-01-12T15:12:56.413 に答える