mardi 2 août 2016

MongoDB mock in node.js?

I am trying to unit test server side code involving functions that make use of mongoDB queries. For now I have created a mock of mongoDB to test my functions. Some of the functions involve complex queries such as collection('collectionName').aggregate(['some values']) or collection..sort('some exp').toArray(callback) which becomes complex. I have tried mocking the monngoDB using mongomock, but it supports limited functions. I have tried this:

var dbMockup = {
    data : null,
    db : function(dbname){
        var that = this;
        return{
            database : dbname,
            collection : function(cname){
                return{
                    collection : cname,
                    aggregate : function(parts, callback){
                                callback(null, that.data);
                    }
                };
            }
        };
    }
};

I have functions such has this (shown below) where I have to write test case for error condition:

db(database).collection('reviews').aggregate([//some values], function(err, data) {
            if(err) {

                callback(err); return;
            }
            callback(null,data);
        });

I checked using mock-mongo-db, but this has also supports the same functions as that of mongomock. Any good mocking library that will help me to test the queries involving functions such as collection('collectionName').aggregate(['value1','value2']) ?

Aucun commentaire:

Enregistrer un commentaire