Mocha断言失败导致超时 [英] Mocha failed assertion causing timeout

查看:97
本文介绍了Mocha断言失败导致超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用NodeJS进行摩卡测试框架.成功断言可以正常工作,但是如果断言失败,则我的测试超时.对于断言,我已经尝试过应该"和期望".例如(异步代码)

I'm getting started with mocha testing framework with NodeJS. Success assertions working fine but if the assertion fails, my test timeouts. For asserting I've tried Should and Expect. For example (async code)

  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,则测试超时,而不是报告失败的断言.我确定我做错了什么.感谢你的帮助.谢谢!

Here the if model id is not 1 then the test timesout instead of reporting failed assertion. I'm sure I'm doing something wrong. Appreciate your help. Thanks!

推荐答案

期望正在抛出一个被诺言捕获的错误.添加一个调用完成条件的捕获条件可以解决此问题.

expect is throwing an error that is being caught by the promise. Adding a catch condition that calls done fixes this.

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);
    })
});

这篇关于Mocha断言失败导致超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆