【问题标题】:Chai http Promise never failingChai http Promise 永不失败
【发布时间】:2017-10-16 16:07:59
【问题描述】:

我正在使用 Chai http 和 promise。下面的测试应该失败,但它没有调用 then 函数就通过了。如果我添加 done 参数以等待异步函数完成,它会失败(正确)。我做错了吗?

it('Returns the correct amount of events', function() {
    chai.request(app)
        .get('/api/events/count')
        .then(function(res) {
            throw new Error('why no throw?');
            expect(res).to.have.status(200);
            expect(res).to.be.json;
        })
        .catch(function(err) {
            throw err;
        });
});

【问题讨论】:

    标签: mocha.js chai chai-http


    【解决方案1】:

    当您忘记返回时,您的测试是常青的。所以,你只需要返回 promise 就可以了:

    it('Returns the correct amount of events', function() {
      return chai.request(app)
        .get('/api/events/count')
        .then(function(res) {
            throw new Error('why no throw?');
            expect(res).to.have.status(200);
            expect(res).to.be.json;
        })
        .catch(function(err) {
            return Promise.reject(err);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多