Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 什么';在承诺处理程序中使用断言是一个很好的策略_Node.js_Testing_Promise_Mocha.js - Fatal编程技术网

Node.js 什么';在承诺处理程序中使用断言是一个很好的策略

Node.js 什么';在承诺处理程序中使用断言是一个很好的策略,node.js,testing,promise,mocha.js,Node.js,Testing,Promise,Mocha.js,我想使用mocha测试异步操作返回的值。这些值通过简单的a+承诺提供。不幸的是,.then处理程序现在将吞下assert引发的所有异常,因此无论发生什么情况,测试都将通过: someOp().then(function(result){ // throws, but will be swallowed assert(result.indexOf('I_DONT_WANT_THAT') < 0); done(); }); someOp().then(函数(结果){ //扔掉,但

我想使用mocha测试异步操作返回的值。这些值通过简单的a+承诺提供。不幸的是,
.then
处理程序现在将吞下
assert
引发的所有异常,因此无论发生什么情况,测试都将通过:

someOp().then(function(result){
  // throws, but will be swallowed
  assert(result.indexOf('I_DONT_WANT_THAT') < 0);
  done();
});
someOp().then(函数(结果){
//扔掉,但会被吞下
assert(result.indexOf('I\u don\u WANT\u THAT')<0);
完成();
});

如何正确测试异步操作的结果?我所能想到的就是无休止地传播错误,但在某个时刻,我需要断言抛出一个真正的错误,以使我的测试失败。

如果您返回承诺,摩卡可以处理承诺:

it('value should be 0', function() {
  return Promise.resolve(1).then(function(value) {
    assert(value === 0);
  });
});