Mocha 测试超时
Mocha Tests Are Timing Out
我是 运行ning Node.js 4.0,所以它现在支持生成器。
我试过 gulp-mocha-co,最近还删除了它并升级到 Node 4.0,因为它现在支持生成器。
无论哪种方式,一旦我开始尝试使我的 mocha 测试生成器变得友好,我在添加 * 使我的 mocha 单元测试生成器后所有这些测试都会超时。我注意到它甚至没有 运行 我的测试实现代码。它到达了我测试的 *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 会等待它被调用,而这永远不会发生,因为...
- Mocha 不支持生成器作为回调。它所看到的只是您的回调 returned 一个迭代器。由于 Mocha 没有 运行 迭代器完成,因此永远不会到达
done()
调用。
Mocha 确实 但是支持 return 承诺的功能,作为在其 arg 列表中声明 done
的功能的互斥替代方案。
co 实用程序可以包装迭代多个承诺的生成器,将它们转换为 return 单个承诺的函数。
为了工作,不要在 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
});
}));
希望对您有所帮助!
我是 运行ning Node.js 4.0,所以它现在支持生成器。
我试过 gulp-mocha-co,最近还删除了它并升级到 Node 4.0,因为它现在支持生成器。
无论哪种方式,一旦我开始尝试使我的 mocha 测试生成器变得友好,我在添加 * 使我的 mocha 单元测试生成器后所有这些测试都会超时。我注意到它甚至没有 运行 我的测试实现代码。它到达了我测试的 *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 会等待它被调用,而这永远不会发生,因为... - Mocha 不支持生成器作为回调。它所看到的只是您的回调 returned 一个迭代器。由于 Mocha 没有 运行 迭代器完成,因此永远不会到达
done()
调用。
Mocha 确实 但是支持 return 承诺的功能,作为在其 arg 列表中声明 done
的功能的互斥替代方案。
co 实用程序可以包装迭代多个承诺的生成器,将它们转换为 return 单个承诺的函数。
为了工作,不要在 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
});
}));
希望对您有所帮助!