无法通过grunt使用Mockgoose运行两组测试

时间:2015-12-14 20:39:07

标签: node.js mongodb unit-testing mongoose gruntjs

我有两组mocha测试,两者都使用mockgoose来模拟mongo数据库。如果我从命令行独立运行它们,一切正常。但是如果我在grunt文件中同时进行两个测试,那么第二个测试无法连接到数据库。

这是我的gruntfile:

module.exports = function(grunt) {
    grunt.loadNpmTasks('grunt-mocha-test');
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        mochaTest: {
            unitTests: {
                options: {
                reporter: 'mocha-jenkins-reporter',
                reporterOptions: {
                    junit_report_name: "Unit Tests",
                    junit_report_path: "tests/unitTestResults.xml",
                    junit_report_stack: 1
                },
                quiet: false, 
                clearRequireCache: true
                },
            src: ['tests/unit/dbFunctionMockTests.js']
            }, apiTests: {
                options: {
                reporter: 'mocha-jenkins-reporter',
                reporterOptions: {
                    junit_report_name: "API Tests",
                    junit_report_path: "tests/apiTestResults.xml",
                    junit_report_stack: 1
                },
                quiet: false, 
                clearRequireCache: true
                },
            src: ['tests/apiTests.js']
            }
        }
    });
    grunt.registerTask('default', 'mochaTest');
}

这是连接到模拟数据库的代码(apiTests.js和dbFunctionMockTests.js中的相同

var mockgoose = require('mockgoose');
var mongoose = require('mongoose');
mockgoose(mongoose)

before(function(done) {
    this.timeout(10000);
    console.log('here') //logs in both tests
    mongoose.connect('mongodb://api.test/TestingDB', function(err) {
        console.log('here too') //only shows up in the first test
        done()
    })
})

有谁知道造成这种情况的原因或如何解决?我真的更喜欢能够从一个单一的grunt文件中运行我的所有测试。

0 个答案:

没有答案