[英]Testing Promises with Mocha
我一直在尝试使用Mocha测试以下代码,但我总是得到错误。
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test
我想测试的代码如下。
'use strict'
const Promise = require('bluebird');
const successResponse = {status: 'OK'};
const failResponse = {status: 'FAIL'};
function dbStatusSuccess () {
return new Promise(function(resolve, reject) {
setTimeout(() => {
resolve(successResponse);
}, 2010);
});
}
function dbStatusFail () {
return new Promise(function(resolve, reject) {
setTimeout(() => {
reject(failResponse);
}, 2000);
});
}
module.exports = {
dbStatusSuccess,
dbStatusFail
}
这是我的测试。
'use strict'
const Promise = require('bluebird');
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();
const healthyCheck = require('./healthyCheck');
const resp = {status:'OK'};
const resp2 ={status: 'FAIL'};
describe('healthy-check end point', () => {
it('should return successful response when connected to database', () => {
return healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
return expect(res).to.equal(resp);
}).catch( (err) => {
console.log(err);
return expect(err).to.deep.equal(resp2);
});
});
});
我还在控制台中收到错误{AssertionError:expected {status:'OK'}等于{status:'OK'}。 我相信这是来自loggin,来自.catch函数的错误。
编辑1.从dbStatusSuccess函数中删除了拒绝功能。
问题在于承诺需要2秒才能完成/失败。 如果setTimeout中设置的时间少于2秒,则测试将通过。
测试中的默认超时似乎是2000毫秒。 您的代码显然需要更长时间才能完成 因此,您必须达到超时限制。 如上所述,您不应使用箭头功能,以便您可以安全地访问this
。
然后你可以像这样增加你的超时:
'use strict'
const Promise = require('bluebird');
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();
const healthyCheck = require('./healthyCheck');
const resp = {status:'OK'};
const resp2 ={status: 'FAIL'};
describe('healthy-check end point', () => {
it('should return successful response when connected to database', function() {
this.timeout(3000);
return healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
return expect(res).to.equal(resp);
}).catch( (err) => {
console.log(err);
return expect(err).to.deep.equal(resp2);
});
});
});
然后你的测试应该按预期运行。
'use strict'
const Promise = require('bluebird');
const chai = require('chai');
chai.use(require('chai-string'));
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();
const healthyCheck = require('./healthyCheck');
describe('healthy-check end point', function() {
it('should return successful response when connected to database', function(done) {
const resp = {status: 'OK'};
healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
expect(res).to.equal(resp);
done();
}).catch(done);
});
});
Error: timeout of 2000ms exceeded
descibe
和it
。 更多信息 好吧,我刚发现问题,你的测试很棘手。 您将超时计时器设置为2010ms,但Mocha默认执行时间为2000ms,因此您将始终从Mocha获取错误。
我仍然认为你不应该在返回的promise链中创建.catch块,它会阻止promise链传播。
describe('healthy-check end point', () => {
it('should return successful response when connected to database', () => {
return healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
return expect(res).to.equal(resp);
});
}).timeout(2500); //tell Mocha to wait for 2500ms
});
您应该使用完成回调,例如:
it('reads some file', function(done) {
fs.readFile('someFile.json', function(err, data) {
if (err) return done(err);
assert(data != null, "File should exist.");
done();
});
});
发生了什么是测试('it'函数)在你的诺言结算之前返回; 使用done意味着在promises结算时调用done()之后测试才会完成。
见http://tobyho.com/2015/12/16/mocha-with-promises/
和
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.