javascript - Set up a mocha tests with sinon mocks, with mysql and bluebird promises -


i have project following setup: javascript es6 (transpiled babel), mocha tests, mysql access node-mysql , bluebird promises.

maybe using bluebird babel/es6 first issue, let's explain situation , problem:

my dbrepository object:

let xdate = require('xdate'),   _ = require('lodash'); const promise = require("bluebird"); const debug = require('debug')('dbrepository');  class dbrepository {    constructor(mysqlmock) {     "use strict";     this.mysql = mysqlmock;     if( this.mysql == undefined) {       debug('init mysql');       this.mysql = require("mysql");       promise.promisifyall(this.mysql);       promise.promisifyall(require("mysql/lib/connection").prototype);       promise.promisifyall(require("mysql/lib/pool").prototype);     }      this.config = {       connectionlimit: 10,       driver: 'pdo_mysql',       host: 'my_sql_container',       port: 3306,       user: 'root',       password: '**********',       testdbname: 'db-name'     };     this.pool = this.mysql.createpool(this.config); // <== here error thrown   }    getsqlconnection() {     return this.pool.getconnectionasync().disposer(function (connection) {       try {         connection.release();       } catch (e) {         debug('error on releasing mysql connection: ' + e);         debug(e.stack);       }     });   }    getgoods(queryparams) {     "use strict";      if (queryparams === undefined) {       queryparams = {};     }     if (queryparams.rowcount === undefined) {       queryparams.rowcount = 15;     }      let query = "select id, name my_table";     return promise.using(this.getsqlconnection(), (conn => {       debug('query: ' + query);       return conn.queryasync(query);     }));   } } 

this code works fine me in normal code, when try use int in mocha test, sinon mocking following error typeerror: this.mysql.createpool not function

this test code:

let expect = require('chai').expect,   xdate = require('xdate'),   _ = require('lodash'),   sinon = require('sinon'),   promise = require('bluebird'),   tobemocketmysql = require('mysql');  promise.promisifyall(tobemocketmysql); promise.promisifyall(require("mysql/lib/connection").prototype); promise.promisifyall(require("mysql/lib/pool").prototype);  describe(".inflateoffers(offerpcs, offergroups)", () => {   "use strict";    it('should inflate offers (with offergroups , pricingcluster db rows.', () => {      let offerpcs = json.parse('[... objects ...]');     let offergroups = json.parse('[... objects ...]');     let mock = sinon.mock(tobemocketmysql);     let dbrepo = new dbrepository(mock); // <== here error thrown       let offers = dbrepo.inflateobjects(offerpcs, offergroups);     expect(offers).to.be.an('object')       .and.to.be.an('array')       .to.have.length(1);      expect(offers[0]).to.be.an('object')       .and.not.to.be.an('array')       .to.have.property('a')       .to.have.property('b');    }); }); 

maybe it's not possible mock promisfyed object @ all?

anybody out there experiences in area?

dbrepository hard test because there little going on - make testing easier, need separate concerns. @ least need break business logic (the raw sql queries) own class, this:

class goodsservice {   /**    * constructor - inject database connection service.    *    * @param {object} db - db connection    */   constructor(db) {     this.db = db;   }    getgoods(queryparams) {     if (queryparams === undefined) {       queryparams = {};     }     if (queryparams.rowcount === undefined) {       queryparams.rowcount = 15;     }      let query = "select id, name my_table";     debug('query: ' + query);      return this.db.queryasync(query);   } } 

so you've separated business logic setting database connector. can pass in instantiated database connection, or stub service class tests so:

let assert = require('assert');  describe('goodsservice', () => {   it('should return array', () => {     let stubbeddb = {       queryasync: () => {         return promise.resolve([]);       }     };     let instance = new goodsservice(stubbeddb);      return instance.getgoods()       .then((result) => {         assert(array.isarray(result), 'should return array of something');       });   }); }); 

that's oversimplified should idea. there things note.

you don't need fancy things chai test promises. mocha has built-in support already.

you don't need use magic sinon.mock. instead, keep things simple , "stub" methods need test in dependency. however, use "spy" check correct sql being generated, i'd in integration test.

does help?


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -