我正在使用mocha为我的nodejs代码运行我的单元测试用例。但是当我发现其中一个模块在同时运行所有测试用例时没有加载其依赖模块时,我感到很惊讶,但是当我单独运行这些测试文件时,它工作得很好。我确实探索了每个文件,但是当我尝试运行所有测试文件时,它无法解决为什么会出现问题?以下是详细信息。
Server.js(这是有问题的文件)
var constants = require("./../utilities/Constants");
var log = require('./../utilities/Logger');
var validator = require('validator');
var Utilities = require('./../../common/Utilities');
var documentOperationModule = require('./../persistence/DocumentOperation');
module.exports = {
loadAppConfig: function (callback) {
//load application config from database and export as module.
documentOperationModule.getAllDocuments(constants.APPLICATION_CONFIG_COLLECTION, "_id", -1, function (err, appConfigs) {
if (!err) {
global.appConfig = appConfigs[0];
callback();
}
else
{
callback(new Error("Unable to load app config data error " + err));
}
});
}
};
在一起运行所有测试用例时抛出以下错误
TypeError: documentOperationModule.getAllDocuments is not a function
然而,当我单独运行每个测试用例文件时,它的工作完美。
Profile.test.js
var expect = require('chai').expect;
var sinon = require('sinon');
var Utilities = require('app-root-path').require('/src/common/Utilities');
var Workout = require('app-root-path').require('/src/server/modules/Workout');
var UnitTestUtils = require('app-root-path').require('/src-test/unit/utils/UnitTestUtils');
var constants = require('app-root-path').require('/src/server/utilities/Constants');
var Server = require('app-root-path').require('/src/server/modules/Server');
//Test suit for getUserSchedule()
describe("getUserSchedule()", function () {
it("should get user schedule", function (done) {
var userId = "sddfgsdgf";
//load global config first
Server.loadAppConfig(function () {
expect(global.appConfig.defaultroutine).to.be.not.undefined;
//generate user schedule
Workout.getUserSchedule(userId, function (err, schedule) {
expect(schedule.sequences[0]).to.include("w");
//exit from test
done();
});
});
});
});
DocumentOperation.js
//Load database connection
var DBModule = require('./../persistence/DBModule.js');
var constants = require("./../utilities/Constants");
var AppUtilities = require('./../utilities/AppUtilities');
var Server = require('./../modules/Server');
var Utilities = require('./../../common/Utilities');
var log = require('./../utilities/Logger');
var Crypto = require('./../modules/Crypto');
module.exports = {
// Get all docuemnts for given collection.
getAllDocuments: function (collection, sortby, order, callback) {
//If sortby is not given then use _id as sort order
if (Utilities.isEmpty(sortby)) {
sortby = "_id";
}
//If order direction is not given then use old item to display first
if (Utilities.isEmpty(order)) {
order = -1; //note, -1 means to display old item at last and +1 means to display old item at top
} else {
order = parseInt(order);
}
log.debug({collection: collection}, "Finally doing sort by " + sortby + " and order as " + order);
var sortObj = {};
sortObj[sortby] = order;
//Filter deleted document if any
DBModule.db.collection(collection).find({isDeleted: {$ne: true}}, {password: 0, temppassword: 0}).sort(sortObj).toArray(function (err, docuemnts) {
if (!err) {
/callback(null, docuemnts);
} else {
callback(err, null);
}
});
}
};
在运行一个测试文件和一起运行所有测试文件时,我对行为差异感到惊讶和困惑。
请帮忙。
答案 0 :(得分:0)
我在发布此问题后才发现问题。在阅读帖子时,我意识到“服务器”模块正在加载“DocumentOperation.js模块,DocumentOperation.js也在加载Server.js模块。由于循环性质,我认为它正在创建问题。当我删除未使用的”Server.js“时从“DocumentOperation.js”然后它工作:-)有时只记录问题也有帮助!!!