title: How to test a callback in a callback using sinon?
body: <p>I am trying to get my codes to 100% coverage. </p>
  
  <p>Right now, what i am missing is this line:</p>
  
  <p><code>(err, result) =&gt; this.processDBResult(err, result, id, req, res, CLASS_NAME, METHOD_NAME, stopwatch)</code></p>
  
  <p>and </p>
  
  <pre><code> (accounttype) =&gt; globUtil.getConfig(accounttype, constant.BOOK,
              source =&gt; this.generateData(req, res, stopwatch, className, methodName, result, source))
  </code></pre>
  
  <p>How do i proceed in doing that? </p>
  
  <p>I try stuff like :</p>
  
  <pre><code>getIdSpy.callsFake(() =&gt; {
      sinon.assert.callCount(processDBResultSpy, 4);      
  });
  </code></pre>
  
  <p>But it will always return success and i know that my <code>processDBResultSpy</code> is not called 4 times, only once.</p>
  
  <p>Codes: </p>
  
  <pre><code>exports.getBookInfo = (req, res) =&gt; {
  
      let stopwatch = globUtil.startStopWatch();
  
      const METHOD_NAME = "getBookInfo";
  
      const id = req.params.id;
      const params = [id];
  
      if (globUtil.isId(id)) {
          bookDao.getBookById(params,
              (err, result) =&gt; this.processDBResult(err, result, id, req, res, CLASS_NAME, METHOD_NAME, stopwatch));
  
      } else {
          bookDao.getBookBySelfId(params,
              (err, result) =&gt; this.processDBResult(err, result, id, req, res, CLASS_NAME, METHOD_NAME, stopwatch));
      }
  };
  
   exports.processDBResult = (err, result, id, req, res, className, methodName, stopwatch) =&gt; {
  
      if (err) {
          log.error(err);
  
          this.generateErrorResponse(req, res, 500, err, stopwatch, className, methodName);
  
          return;
      }
  
      if (result.length == 0) {
          log.info("No results");
          this.generateErrorResponse(req, res, 404, format(message.NOT_FOUND, id), stopwatch, className, methodName)
  
          return;
      }
  
      globUtil.getAccountType(id,
          (accounttype) =&gt; globUtil.getConfig(accounttype, constant.BOOK,
              source =&gt; this.generateData(req, res, stopwatch, className, methodName, result, source)));
  }
  </code></pre>
  
  <p>Test case:</p>
  
  <pre><code>it('1. getBookInfo - called with id', (done) =&gt; {
          let getIdSpy = sinon.stub(bookDao, "getBookById");
          let getSelfIdSpy = sinon.stub(bookDao, "getBookBySelfId");
          let processDBResultSpy = sinon.stub(controller, "processDBResult");
  
          let request = httpMocks.createRequest({
              method: 'GET',
              url: constant.MODULE_URL,
              params: {
                  id: 'BOOKID'
              }
          });
  
          let response = httpMocks.createResponse();
          controller.getHdbInfo(request, response);
  
          sinon.assert.callCount(getIdSpy, 1);
          sinon.assert.callCount(getSelfIdSpy, 0);
  
          //how do i proceed form here
  
          done();
      });
  </code></pre>