摩卡失败断言造成超时

我正在开始使用NodeJS的摩卡testing框架。 成功断言工作正常,但如果断言失败,我的testing超时。 为了断言我已经尝试了应该和期望。 例如(asynchronous代码)

it('should create new user', function(done){ userService.create(user).then(function(model){ expect(model.id).to.be(1); //created user ID done(); }, done) }); 

在这里,如果模型id不是1,那么testing超时而不是报告失败的断言。 我确定我做错了什么。 感谢你的帮助。 谢谢!

期望抛出一个被承诺所捕获的错误。 添加捕获条件,调用完成修复此。

 it('should create new user', function(done) { userService.create(user).then(function(model) { expect(model.id).to.be(1); //created user ID done(); }).catch(function(e) { done(e); }) }); 

肖恩的答案是有效的,但有一个更简单的方法。

如果您从testing中返回诺言,摩卡将为您处理所有事情:

 it('should create new user', function() { return userService.create(user).then(function(model){ expect(model.id).to.be(1); //created user ID }); }); 

没有donecallback需要!

看起来好像从来没有叫过。 除此之外,你可能还需要else来处理失败。