mercredi 19 août 2015

Unit testing mongojs error with sinon

I'm attempting to unit test my error paths for mongojs.

File to be tested looks something like this:

var mongojs = require('mongojs');
var db = mongojs('localhost/local', ['mydb']);
exports.dbCall = function(myID, cb){
    db.mydb.findOne{_id: mongojs.ObjectId(myID)}, function(err, entry){
        if(err){
           cb(err);
        } else {
           cb(null, entry);
        }
    });
}

unit test file:

var orig_file  = require ('orig_file.js'),
assert = require('assert'),
sinon  = require('sinon'),
mongo  = require('mongojs'),
db     = mongo('localhost/local', ['mydb']);

sinon.stub(db.mydb, 'findOne').yields('error');

orig_file.dbCall(<object id>, function(err, doc){
    assert(err);
});

db.mydb.findOne.restore();

I've made a few attempts altering the function being stubbed, but with no luck. If I attempt to stub anything else I usually get TypeError: Attempted to wrap object property as function

Aucun commentaire:

Enregistrer un commentaire