【发布时间】:2016-03-09 20:18:11
【问题描述】:
我有一组测试,格式如下:
var mongoTest = require('../mongoTest.js');
//Connect to test DB before tests and disconnect after
before(function(done) {
mongoTest.mongoConnect(done);
});
after(function(done) {
mongoose.disconnect(done);
})
//Load Data Files
var testData = require('../testData.js')
var deviceAndLocationAnswers = testData.deviceAndLocationAnswers
//Repeated Functions:
var clearCollections = function(coll, callback) {
mongoose.connection.db.dropCollection(coll.collectionName,
function(err, result) {
callback();
});
}
describe('Testing functions that use the Breakers collections', function(){
//Test Setup
var req = {query: {device: testData.powerConsumptionDevice}}
before(function(done) {
this.timeout(15000);
async.forEach(mongoose.connection.collections, clearCollections,
function(err) {
if (err) {console.log(err)};
done();
})
});
before(function(done) {
this.timeout(15000);
Breakers.create(testData.breakersData, function(err, model){
done(err);
});
});
after(function(done) {
this.timeout(15000);
async.forEach(mongoose.connection.collections, clearCollections,
function(err) {
if (err) {console.log(err)};
done();
})
});
// Tests
describe('Testing powerConsumption Function', function() {
it('Should produce some output', function(done) {
this.timeout(15000);
dbFunctions.powerConsumption(req, function(result) {
result.should.exist;
done();
});
});
it('Should produce the same results as the mock up from testData', function(done) {
this.timeout(15000);
dbFunctions.powerConsumption(req, function(result) {
result.should.be.deep.equal(testData.powerConsumptionResults);
done();
});
});
});
});
mongoTest 来自以下文件:
var mongoose = require('mongoose')
var dBaseURL = 'mongodb://xxxx:yyyyy@ds#####.mongolab.com:zzzz/myDB'; // details removed
exports.mongoConnect = function(callback) {
mongoose.connect(dBaseURL, function(err) {
if(err) {
console.log('MongoDB Connection Error', err);
} else {
console.log('MongoDB Connection Successful')
}
callback();
});
};
我还有另一个文件,我在其中使用 mockgoose 模拟数据库,以及仅测试简单函数的第三个测试文件
当我单独运行这些测试时,它们会成功运行。但是当我尝试用 grunt 运行所有三个时,在处理 Mongo 的一个测试中,一切都挂在第一个调用之前。没有连接到 Mongo 数据库。我什至无法通过在连接回调中放置一个 console.log() 来获取信息。
这里是 grunt 文件:
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-mocha-test');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Configure a mochaTest task
mochaTest: {
jenkins: {
options: {
reporter: 'spec',
captureFile: 'tests/results.txt', // Optionally capture the reporter output to a file
quiet: false, // Optionally suppress output to standard out (defaults to false)
clearRequireCache: true
},
src: ['tests/unit/*.js']
}
}
});
grunt.registerTask('default', 'mochaTest');
};
我已经使用 NPM 进行了全新安装。我的测试失败的具体机制是第一个 before 在 30 秒后超时。
有谁知道如何解决这个问题?我觉得我错过了一些关键步骤,但我一直在谷歌搜索一个小时的解决方案,但没有运气。我尝试将 clear required cache 设置为 true 和 false,但是这两个选项似乎都没有做任何事情。
【问题讨论】:
标签: node.js mongodb mongoose gruntjs mocha.js