[英]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.