简体   繁体   中英

How can I unit test this node.js module using mocha and chai?

I have this file containing the following code which is the database layer of my api. It is externally dependent on SQL Server to fetch the data.

var sql = require('mssql');
var async = require('async');

module.exports = {
    getDetails: function(number, callback) {

    async.parallel({
            general: function(callback) {
                getGeneral(number, callback);
            },
            preferences: function(callback) {
                getPref(number, callback);
            }
        },
        function(err, results) {
            if (err) {
                logger.error(err);
                throw err;
            }
            callback(results);
        });
}
};

function getGeneral(number, callback) {
    var mainconnection = new sql.Connection(dbCredentials[1].generalDBConfig, function(err) {
        var request = new sql.Request(mainconnection);
        request.input('number', sql.BigInt, number);
        request.execute('[number].[genral_get]', function(err, recordsets) {
            if (err) {
                logger.error(err);
            }
            var gen = {};
            var spResult = recordsets[0];
            if (spResult[0] != null) {
                spResult.forEach(function(record) {
                    var test = {};
                    gen[record.genCode] = record.genValue;
                });

                callback(null, gen);
            } else {
                callback(null, null);
            }

        });

    });
}

function getPref(number, callback) {
    var mainconnection = new sql.Connection(dbCredentials[0].prefDBConfig, function(err) {
        var request = new sql.Request(mainconnection);
        request.input('number', sql.BigInt, number);
        request.execute('[number].[pref_get]', function(err, recordsets) {
            if (err) {
                logger.error(err);
            }
            var spResult = recordsets[0];
            if (spResult[0] != null) {
                callback(null, spResult[0]);
            } else {
                callback(null, null);
            }

        });

    });
}

The database layer return this JSON format data:

{
                        "general": {
                            "number": "72720604"
                            "title": "Mr  ",
                            "buildingNameNumber": null,
                            "subBuildingName": null,
                            "streetName": null,
                            "postalTown": null,
                            "county": null
                        },
                        "pref": {
                            "UseAccessibilitySite": "00000",
                            "IntroductorySource": "75"
                        }
                    };

As I am new to unit testing, I don't know how to start about writing unit tests for this module even though choosing mocha with chai as my unit testing framework. Any kind of suggestions or help is appreciated...

The question is: what exactly do you want to test in this code?

I'm assuming that the answer roughly is "I want to call getDetails() in my test suite and verify that it behaves correctly".

Of course, you don't want to create a whole MSSQL server in your test suite. Instead, it's much easier to stub the database.

A module for mocking, stubbing and spying that I've had great success with is sinon.js . For your code, you'll need to stub a few things:

  • Stub sql.Connection() to return a stubbed connection object.
  • Stub sql.Request() to return a stub object that has methods input and execute .
  • Stub sql.BigInt() to verify that it was called correctly.

Then, for the stubbed request object, you need to:

  • Verify that .input() was called correctly.
  • Make sure that execute returns the record(s) of your choosing.

It will be a lot of setup work to test the getDetails() function entirely. You might also be interested in the rewire module, which allows you to test getGeneral() and getPref() directly without having to add them to module.exports .

Finally, refactoring this code into smaller pieces will also help a lot. For example, if you could do something like this:

// This is your own db module, which takes care of connecting
// to the database for you.
var db = require('./db');

function getGeneral(number, callback) {
    var request = db.createRequest();
    // The rest of the function
}

Then it would be much easier to use sinon to create a fake request object that does exactly what you want it to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM