node.js - Mockgoose: how to simulate a error in mongoose? -


i trying unit test around mongoose powered application. while mockgoose great work @ simulating mongoose can test around it, didn t find way push fail call, can test error handling logic.

is supported use case? or should find framework?

code:

var mongoose = require('mongoose');  var test = {},     doc = require('./model/doc.js');  var dburl = 'mongodb://127.0.0.1/',     dbname = 'test';  function connect(callback) {     test = mongoose.createconnection(dburl + dbname); //<-- push fail     test.on('error', (err) => {         callback(err);     });     test.once('open', function () {         callback();     });  } 

test:

var chai = require('chai'),     expect = chai.expect,     util = require('util');  var config = require('./config.json');  var proxyquire =  require('proxyquire').nopreservecache(); var sinon = require('sinon'); var mongoose = require('mongoose'); var mockgoose = require('mockgoose');  describe('test', () => {     describe('connect', () => {         beforeeach((callback) => {             mockgoose(mongoose).then(() => {                 callback();             });         });          it('expect connect database', (done) => {             var stub = {                     mongoose: mongoose                 },                 test = proxyquire('./../test.js', {                     'mongoose': stub.mongoose                 });              test.connect((err) => {                 try {                     expect(err).to.not.be.ok;                     done();                 } catch(err) {                     done(err);                 }             });          it('expect throw error when failing connect database', (done) => {             var stub = {                     mongoose: mongoose                 },                 medical = proxyquire('./../medical.js', {                     'mongoose': stub.mongoose                 });              medical.connect((err) => {                 try {                     expect(err).to.be.ok;                     done();                 } catch(err) {                     done(err);                 }             });         });     }); }); 

result in:

connect       ✓ expect connect database       1) expect throw error when failing connect database     1 passing (234ms)   1 failing    1) medical connect expect throw error when failing connect database:      assertionerror: expected undefined truthy 

i ended stubing mongoose.createconnection (so generator) return object call error.

            let stub = {                 mongoose: {                     createconnection: () => {                         return {                             on: (eventname, callback) => {                                 if(eventname === 'error') {                                     callback('medical error');                                 }                             },                             once: () => {}                         }                     }                 }             },             medical = proxyquire('./../medical.js', {                 'mongoose': stub.mongoose             }); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -