处理磁带测试中的错误?

时间:2015-10-11 13:26:54

标签: node.js testing tap node.js-tape

如果我有一个抛出错误的函数并且我想测试该错误,我会写这样的东西:

test('throws at something that is not a string', t => {
  t.plan(1)
  t.err(loadString(9))
})

但这总是导致执行时来自函数的实际错误:

还有not ok 1 no plan foundnot ok 2 no assertions found,这也很奇怪。我怎样才能确定它实际上没有抛出?

1 个答案:

答案 0 :(得分:6)

如果你想测试函数是否会抛出,你必须使用t.throws并传递一个函数(不是错误值)。在这里,我假设loadString是你正在测试的实际函数,所以你实际上是在抛出它而不是磁带调用它并捕获错误。

请改为尝试:

t.throws(function() { loadString(9); });