0

I'm working with mongojs and writing tests for mocha running coverage with istanbul. My issue is that I would like to include testing db errors.

var mongojs = require('mongojs');
var db = mongojs.connect(/* connection string */);
var collection = db.collection('test');

...
rpc.register('calendar.create', function(/*... */) {
    collection.update({...}, {...}, function (err, data) {
        if (err) {
            // this code should be tested
            return;
        }

        // all is good, this is usually covered
    });
});

the test looks like this

it("should gracefully fail", function (done) {

    /* trigger db error by some means here */

    invoke("calendar.create", function (err, data) {
        if (err) {
            // check that the error is what we expect
            return done();
        }

        done(new Error('No expected error in db command.'));
    });
});

There is a fairly complex setup script that sets up the integration testing environment. The current solution is to disconnect the database using db.close() and run the test resulting in an error as wanted. The problem with this solution arises when all the other tests after that require the database connection fail, as I try to reconnect without success.

Any ideas on how to solve this neatly? Preferably without writing custom errors that might not be raised by next version of mongojs. Or is there a better way of structuring the tests?

hg.
  • 324
  • 1
  • 13

1 Answers1

0

What about mocking the library that deals with mongo?

For example, assuming db.update is eventually the function that gets called by collection.update you might want to do something like

describe('error handling', function() {

  beforeEach(function() {
    sinon.stub(db, 'update').yields('error');  
  });

  afterEach(function() {
    // db.update will just error for the scope of this test
    db.update.restore();
  });

  it('is handled correctly', function() {
    // 1) call your function

    // 2) expect that the error is logged, dealt with or 
    // whatever is appropriate for your domain here
  });

});

I've used Sinon which is

Standalone test spies, stubs and mocks for JavaScript. No dependencies, works with any unit testing framework.

Does this make sense?

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • It does make sense. However, I was kind of hoping for some built-in error triggering, which would simulate a range of actual errors. I'll just let this question hang around for a little while longer, before I accept this as an answer, but thanks for the quick response! – hg. Mar 18 '15 at 11:54
  • I see that @AndreasNiedermair edited my question to remove the reference to mongojs, to the more generic case of just testing, which in this case might not really be what I was after. I guess my problem is not as common as I expected. – hg. Mar 18 '15 at 12:08
  • I think that if you're trying to test for error handling the easiest way is to mock and have an error returned to you, but it's just IMHO... I don't know your scenario. – Alberto Zaccagni Mar 18 '15 at 12:26