5

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

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

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

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

4

1 回答 1

7

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

试试这个:

t.throws(function() { loadString(9); });
于 2015-11-15T23:49:16.613 回答