【问题标题】:Mocha MongoDB (Mongoose) tests hanging when run with Grunt使用 Grunt 运行时,Mocha MongoDB (Mongoose) 测试挂起
【发布时间】: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


    【解决方案1】:

    我不确定是什么特定的交​​互导致我的测试失败,但如果我将模拟和非模拟测试分成它们自己的对象,它们就会成功。

    这是有效的代码:

    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');
    };
    

    【讨论】:

      猜你喜欢
      • 2016-01-08
      • 2018-07-21
      • 2018-12-05
      • 2016-06-25
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多