[英]Mocha Tests Are Timing Out
我正在运行Node.js 4.0,因此它现在支持生成器。
我已经尝试过gulp-mocha-co,最近还删除了它,并升级到Node 4.0,因为它现在支持生成器。
无论哪种方式,一旦我开始尝试使我的摩卡测试生成器变得友好,在添加*使我的摩卡单元测试生成器之后,我就会在所有这些测试上超时。 我注意到它甚至没有运行我的测试实现代码。 它到达我测试的* function(),这就是它坐下并超时的时候。
我现在正在使用gulp-mocha 。
myTests.js
"use strict";
var chai = require('chai'),
should = chai.should(),
testUtil = require('../../../test/testUtilities'),
carUseCase = require('../../../src/usecases/carGet'),
gateway= require('../../../test/gateway'),
carRequestModel = require('../../../src/models/http/request/carRequest');
describe('Get Car by Id', function() {
it('should return no car when no cars exist', function*(done){
var cars = [];
inMemoryGateway.data(cars);
carUseCase.gateway(gateway);
var request = testUtil.createCarRequest();
var responseModel = yield carUseCase.find(request);
should.not.exist(responseModel.cars);
var request = testUtil.createCarRequest(0, "", "", "");
var responseModel = yield carUseCase.find(request);
should.not.exist(responseModel.cars);
done();
});
gulp.js
var gulp = require('gulp'),
mocha = require('gulp-mocha');
...
gulp.task('mocha-unit', function() {
process.env.PORT = 5001;
return gulp.src([config.test.src.unit], { read: false })
.pipe(mocha({
reporter: config.test.mocha.reporter,
ui: 'bdd'
}))
});
carGet.js
var car = require('../entities/car'),
realGateway = require('../../src/gateways/carGateway'),
carResponse = require('../../src/models/http/response/carResponse'),
_gateway;
module.exports = {
find: function *(carRequest){
carResponse.http.statusCode = 200;
var entity = yield _gateway.find(carRequest.id);
if(!entity.cars || entity.cars.length == 0){
entity.cars = null;
carResponse.http.statusCode = 204;
}
carResponse.cars = entity.cars;
return carResponse;
}
};
gatewayTestDouble.js
'use strict';
var _data;
module.exports = {
data: function(data){
_data = data
},
find: function *(id) {
var found = [];
if(id == null && hasData(_data)){
yield _data;
return;
}
if(!id && !isPositiveNumber(id)){
yield found;
return;
}
if(isPositiveNumber(id) && hasData(_data)) {
for (var i = 0; i < _data.length; i++) {
if (_data[i].id === id)
found.push(_data[i]);
}
}
yield found;
}
};
错误
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
这里发生了几件事。
done
声明为回调参数,因此Mocha等待它被调用,这永远不会发生,因为... done()
调用。 但是,Mocha 确实支持返回承诺的函数,作为在arg列表中声明done
函数的互斥替代。
co实用程序可以包装可迭代多个promise的生成器,将它们转换为返回单个promise的函数。
为了工作,请不要在arg列表中声明done
,然后导入co
并执行以下操作:
it('should foo', co.wrap(function*() {
var foo = yield somethingThatReturnsAPromise();
// do something with foo
}));
请注意,您也可以执行以下操作,而Mocha无法分辨出区别:
it('should foo', co.wrap(function*() {
return somethingThatReturnsAPromise().then(function(foo) {
// do something with foo
});
}));
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.