【问题标题】:Handling errors in a Tape test?处理磁带测试中的错误?
【发布时间】:2015-10-11 13:26:53
【问题描述】:

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

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,这也很奇怪。我怎样才能确保它实际上没有抛出?

【问题讨论】:

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


    【解决方案1】:

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

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 2016-02-16
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2018-04-19
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多