如何使用sinon在节点中存根uncaughtException监听器

问题描述 投票:0回答:1

我有一个自定义处理程序,用于我想要测试的未捕获的异常。通过所有尝试,我也尝试了一个分叉的子进程。

这是一个人为的例子......

process.on('uncaughtException', function(err) {
  otherFunction(err.message);
});

it('should catch exceptions', function() {
  stub(otherFunction);

  throw new Error('foo');

  assert.calledWith(otherFunction, 'foo');
});

有什么我需要使用我的存根工具(目前使用sinon),或者这是完全错误的方法?

node.js sinon stub
1个回答
1
投票

itfunction中抛出的任何错误都应该使该测试失败。您可以使用try {}包装throw语句,并且在catch块中您可以编写assert函数。

© www.soinside.com 2019 - 2024. All rights reserved.