1

I have a function called connectToMongo that connects to a locally-running mongodb database myapp. I have another function which invokes connectToMongo. I would like to test this other function with mocha. However, I would like to make it so that, while testing, any calls to connectToMongo will automatically connect to my myapp_test database instead of my normal one.

Ideally (I think) I'd like to do something like this (where connectToMongoTestingDatabase connects to myapp_test):

var connectToMongo = require('./connectToMongo'),
    connectToMongoTestingDatabase = require('./connectToMongoTestingDatabase')
    registerUser = require('./registerUser');

testingLibrary.configureAlternateBehavior(connectToMongo, connectToMongoTestingDatabase);

it('should register a user', function (done) {

    registerUser({name: 'bob', password: 'password1'}, function (error, response) {
        connectToMongoTestingDatabase(function (error, database) {
            database.collection('users').findOne({name: 'bob'}, function (error, user) {
                expect(user).not.to.be.null;
                done();
            });
        });
    });

});

I have been trying to make sense of the Sinon.JS library. I can't tell whether spies, stubs or mocks would fulfill this need, nor what the equivalent of testingLibrary.configureAlternateBehavior would be.

I suppose I could also create some global variable called global.MYAPP_TESTING, set that to true before I start my tests, and in the implementation of connectToMongo I could connect to the myapp_test database if global.MYAPP_TESTING is true. That seems bad though.

What is the best way to substitute the behavior of one function for another in JavaScript unit testing?

Stennie
  • 63,885
  • 14
  • 149
  • 175
Jackson
  • 9,188
  • 6
  • 52
  • 77
  • This is often what the DI (Dependency injection) pattern is used for. Make a single-use set of functions you just `require()` in place of the mongo business. – SamT Aug 23 '14 at 16:40

1 Answers1

1

If you are running a unit test, then you should mock all external dependencies (functions, objects) used in your function and just check that they are called or got desired values.

To make possible integration testing in your ConnectToMongo function you should get the connection string to db from environment depending config. So when you'll run the test you'll set environment to 'test' and get one config object, during development you may have another config object (which different settings) and on live environment a 3d config. Check out this for more info on making environment dependent config: Node.js setting up environment specific configs to be used with everyauth

Community
  • 1
  • 1
Denis Shulepov
  • 381
  • 1
  • 6
  • Ended up doing `process.env.NODE_ENV = 'test'` before running mocha, and then using a config script that sets the correct mongo url based on the value of `NODE_ENV`, and requiring that config in connectToMongo when connecting. – Jackson Aug 23 '14 at 19:00