我写了一个非常简单的函数,它返回一个promise。当promise解析时,它返回数据库中的所有trip对象。当我在一个then函数的console.log结果中,我看到了所有的对象,所以我知道这个函数有效。但是当我尝试测试时,我得到了这个"超时"例外。我编写了另一个函数,它返回一个简单的promise对象,省略了所有的mongoose和数据库连接对象,并且它抛出了同样的错误。我认为这是关于在mocha中解决承诺而不是关于mongoose或数据库连接。
任何人都可以帮我找到这里的问题,或者我该如何继续调查这个问题?
提前致谢。
var mongoose = require('mongoose');
require('../models/Trip');
mongoose.connect('mongodb://localhost:27017/gtfs-dev');
var mongodb = mongoose.connection;
var Trip = mongodb.model('Trip');
var findTrips = function () {
var query = Trip.find({},{"trip_id":1, "_id":0});
var promise = query.exec();
return promise;
}
processRoutes.spec.js:
...
mongoose.Promise = global.Promise;
var processroutes = require('./processRoutes');
describe('ProcessRoute', function () {
it('dependencies should exist', function () {
expect(processroutes).to.be.not.undefined;
});
describe('#findTrips()', function(){
it('should exist and be a function', function(){
expect(processroutes.findTrips).to.be.not.null;
expect(processroutes.findTrips).to.be.a('function');
});
it('should return a promise', function(){
var promise = processroutes.findTrips();
expect(promise).to.be.a('promise');
});
it('should resolve to an array', function(){
return processroutes.findTrips().then(function(trips){
expect(trips).to.be.instanceof(Array);
});
});
});
});
测试结果如下:
ProcessRoute
✓ dependencies should exist
#findTrips()
✓ should exist and be a function
✓ should return a promise
1) should resolve to an array
3 passing (5s)
1 failing
1) ProcessRoute #findTrips() should resolve to an array:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.