jasmine-node async不理解代码错误

时间:2015-09-03 21:11:49

标签: node.js jasmine-node

我正在使用async尝试jasmine-node:

it('should not be dumb', function(done){
Promise.resolve(0).then(function(){
  console.log('dumb');
  expect(2).toEqual(2);
  done();
});

});

返回:

dumb
.

Finished in 0.026 seconds
1 test, 1 assertion, 0 failures, 0 skipped

但不知何故,如果我的代码中有错误:

  it('should not be dumb', function(done){
  Promise.resolve(0).then(function(result){
    console.log('dumb');
    expect(2,toEqual(2));
    done();
    }, function (err){console.log(err); done()});
  });

它只是坐在那里,直到它超时,没有任何有用的输出:

dumb

Pairing user stories - 7816 ms
    should not be dumb - 7815 ms

Failures:

  1) Pairing user stories should not be dumb    Message:
     timeout: timed out after 5000 msec waiting for spec to complete    Stacktrace:
     undefined

Finished in 46.884 seconds 1 test, 1 assertion, 1 failure, 0 skipped

我做错了什么?

1 个答案:

答案 0 :(得分:2)

假设:

promise.then(onResolve, onReject)
onReject被拒绝时,将会调用

promise。但是,在您的代码中,拒绝发生在onResolve(隐式地,通过抛出错误),这不会影响promise(此时已经解决),因此不要拨打onReject

如果你想捕捉onResolve中抛出的错误,你需要通过在你的承诺链中添加另一个拒绝处理程序来处理它们:

Promise.resolve(0).then(function(result){
  console.log('dumb');
  expect(2,toEqual(2));
  done();
}).then(null, function (err) {
  console.log(err);
  done()
});

如果Promise恰好是bluebird,您可以使用.catch()稍微缩短它:

Promise.resolve(0).then(function(result){
  console.log('dumb');
  expect(2,toEqual(2));
  done();
}).catch(function (err) {
  console.log(err);
  done()
});