MongoDB Connection Failure to Test MEAN Application Modules

I acted out my brains trying to figure out how to handle mocking my MongoDB connection for my unit tests. I'm wondering which method is right for this, as there might be a problem with the layout of the application. This is for a larger project with lots of modules.

General layout

package.json server.js -models -index.js -users.js -events.js -... -services -index.js -userActivity.js -... +public +routes +util +test 

models / users.js

 "use strict"; modules.export = function(mongoose) { var Schema = mongoose.Schema; var userSchema = new Schema({ name: String }); var userModel = mongoose.model('User', userSchema); var userDAO {}; userDAO.addUser(user) { var newUser = new userModel(user); return newUser.save(); } userDAO.getUser(id) { return userModel.findById(id).lean().exec(); } return userDAO; }; 

models / index.js

 "use strict"; var bluebird= require('bluebird'); var mongoose = bluebird.promisifyAll(require('mongoose')); var Users = require ('./users'); var Events = require('./events'); module.exports.Users = new Users(mongoose); module.exports.Events = new Events(mongoose); 

services / userActivity.js

 "use strict"; var db = require('../models'); module.exports = function(userID) { return db.Events.getEventsForUser(userId) }; 

Now here where is the problem

test / test.js

 "use strict"; var chai = require('chai'); chai.use(require('chai-as-promised'); var expect = chai.expect; var mongoose = require('mongoose'); mongoose.connect("mongodb://localhost/testDB") var db = require('../models'); var services = require('../services') describe("sample tests", function() { var user1, user2; var user1TestEvents = []; before(function(done) { db.Users.addUser({name:"John"}). then(function(john) { user1 = john; return db.Users.addUser({name: "Mary"}); }). then(function(mary) { user2 = mary; return db.Events.addEvent{user: user1._id, event: "logged in", time: new Date()}); }). then(function(event) { user1TestEvents.push(event); done() }); }; it('gets a users events', function() { var events = services.getEventsForUser(user1._id); return expect(events).eventually.to.have.length(user1TestEvents.length); }); 

Tests work very well if I connect to the real-time database on my own machine, but our CI servers will not have a mongo database on them. I tried to figure out some way to fake the connection, but I did not find any good alternatives

I am trying to use Mockgoose to mock a database, but if I do not pass the mockgooed mongoose object completely through all the components, it will not work.

 var mockgoose = require('mockgoose'); var mongoose = require('mongoose'); mockgoose(mongoose); mongoose.connect("mongodb://localhost/test"); 

When I tried this, after the first promise is resolved in my "before", all subsequent promises will never be eliminated and mocha will not be executed.

I also failed to get tingoDB to work, and I feel like I'm missing something obvious.

All the examples that I saw were very simple test cases when the models were created in the same place where the mongoose object was connecting and with how I laid out the application. I cannot get a disconnect from mongoose to the actual instance of MongoDB.

If anyone has experience with unit testing MEAN applications without a real MongoDB instance, I would be very pleased with some tips on how to fix my layout and get my unit tests.

+5
source share
1 answer

You can use rewire to make fun of any required module. And revising the method you need.

+1
source

All Articles