我有以下格式的一组测试:
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数据库。我甚至无法通过在connect回调中放置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进行了全新安装。我的测试失败的具体机制是30秒后第一次before
超时。
有谁知道如何解决这个问题?我觉得我错过了一些关键步骤,但我一直在谷歌搜索一小时没有运气的解决方案。我已经尝试将明确要求的缓存设置为true和false,但两种选择似乎都没有做任何事情。
答案 0 :(得分:0)
我不确定哪些特定的交互导致我的测试失败,但如果我将模拟和非模拟测试分成他们自己的对象,它们就会成功。
以下是有效的代码:
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-mocha-test');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
mochaTest: {
unitTests: {
options: {
reporter: 'spec',
captureFile: 'tests/unitTestResults.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/helpersTest.js', 'tests/unit/dbFunctionTests.js']
}, mockedUnitTests: {
options: {
reporter: 'spec',
captureFile: 'tests/mockedUnitTestResults.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/dbFunctionMockTests.js']
}
}
});
grunt.registerTask('default', 'mochaTest');
};