title: Mocking connection pool and test url param using sinon?
body: <p>I am doing a RESTful api using node.js, </p>
  
  <p>Unit Testing using mocha, sinon, chai.</p>
  
  <p>DB using ibm DB2.</p>
  
  <p><strong>Problem</strong>:</p>
  
  <p>I am trying to a unit test on the getNews method. </p>
  
  <p>I know the correct way of doing a unit test is not to access the database. So I am trying not to access the database. </p>
  
  <p><strong>Q1:</strong>
  However, how do I do a mock on <code>pool.open(connString, function (err, db)</code> to return a fake result, assuming that I created the json result. </p>
  
  <p>Assuming that my newsId that I want to get is 999.</p>
  
  <p><strong>Q2:</strong>
  How do it ensure that the newsId i passed in is 999 and not another value?</p>
  
  <p><strong>Codes:</strong></p>
  
  <p>These are files that are already running correctly.</p>
  
  <p>newsRest.js:</p>
  
  <pre><code>module.exports = (app) =&gt; {
  
      const controller = require('../controller/newsController');
  
      app.route('news/:newsId').get(controller.getNews);
  
  };
  </code></pre>
  
  <p>newsController.js:</p>
  
  <pre><code>'use strict';
  
  //imports
  var connectionString = require('../common/ibmdb2Pool').connectionString;
  var connString = connectionString();
  
  var initPool = require('../common/ibmdb2Pool').initPool;
  var pool = initPool();
  
  const query = require('../db/query');
  
  /*
   * Restful API starts here
   * */
  
  //get news by id
  
  exports.getNews = (req, res) =&gt; {   
  
      //get from request
      const newsId = req.params.newsId;
      const params = [newsId];
  
      pool.open(connString, function (err, db) {
          if (err) {
              return console.log(err);
          }
  
          db.query(query.sqlSelect, params, function (error, result, info) {
  
              if (error) {
                  console.log(error); 
                  return false;
              }
  
              res.status(200).json({
                  result: result
              });
          });
      });
  };
  </code></pre>
  
  <p>ibmdb2Pool.js </p>
  
  <pre><code>'use strict';
  
  //imports
  require('dotenv').config();
  
  var Pool = require("ibm_db").Pool;
  
  // access the environment variables for this environment
  var connString = process.env.CONNSTRING;
  
  //variables
  const minPoolSize = 20;
  const maxPoolSize = 100;
  
  //return connection string from environment file.
  exports.connectionString = function () {
      return connString;
  };
  
  //init the db pool with a min and max size
  exports.initPool = function () {
      var pool = new Pool();
  
      var ret = pool.init(minPoolSize, connString);
  
      if(ret !== true) {
  
          console.log("Cannot init pool. " + ret);
      }
  
      pool.setMaxPoolSize(maxPoolSize);
  
      return pool;
  };
  </code></pre>
  
  <p>Here is my Unit Test class:</p>
  
  <p>newsController.spec.js</p>
  
  <pre><code>process.env.NODE_ENV = 'test';
  
  var initPool = require('../common/ibmdb2Pool').initPool;
  var pool = initPool();
  
  const sinon = require('sinon');
  const request = require('request');
  const chai = require('chai');
  const chaiHttp = require('chai-http');
  const should = chai.should();
  let server = require('../../../main.js');
  
  chai.use(chaiHttp);
  
  describe('news service', () =&gt; {
          beforeEach(() =&gt; {
              this.open = sinon.stub(pool, 'open');
  
              });
          });
  
          afterEach(() =&gt; {
              pool.open.restore();
          });
  
          it('should list a SINGLE news /news/newsId GET', (done) =&gt; {
  
              chai.request(server) 
                  .get('/news/999')
                  .end(function(err, res){
                      res.should.have.status(200);
                      res.should.be.json;
                      res.body.should.be.a('object');                                 
                      res.body.result.should.include.keys(
                              'NEWS_ID', 'TITLE', 'NEWS_TYPE', 'DESCRIPTION', 'CREATED_BY', 'CREATED_DATE'
                            );
                     done();
                 })
          });
  });
  </code></pre>