title: Sinon Spy / Stub for Function inside Function (Private Function)
body: <p>I'm new <code>Sinon</code>. i'm unable to spy the private <code>ajax</code> function</p>
  
  <h3>Actual code in <code>library.js</code></h3>
  
  <pre><code>function ajax () {
      console.log("I'm a");
  }
  
  function getJSON() {
      ajax();
      console.log("I'm b");
  }
  
  exports.getJSON = getJSON;
  </code></pre>
  
  <h3>Actual Test Code</h3>
  
  <pre><code>var sinon = require('sinon');
  var lib = require('./library');
  
  describe('Tutor Test', function () {
      it('getJSON Calling ajax', function (done) {
          var ajax = sinon.spy(lib, 'ajax');
          lib.getJSON();
  
          ajax.restore();
          sinon.assert.calledOnce(ajax);
          done();
      });
  });
  </code></pre>
  
  <blockquote>
    <p>Note: I already tried with below object example. it works like charm.</p>
  </blockquote>
  
  <h3>Working Code in <code>library.js</code></h3>
  
  <pre><code>var jquery = {
      ajax: function () {
          console.log("I'm a");
      },
  
      getJSON: function () {
          this.ajax();
          console.log("I'm b");
      }
  };
  
  exports.jquery = jquery;
  </code></pre>
  
  <h3>Working Test Cases.</h3>
  
  <pre><code>var sinon = require('sinon');
  var $ = require('./library').jquery;
  
  describe('Tutor Test', function () {
      it('getJSON Calling ajax', function (done) {
          var ajax = sinon.spy($, 'ajax');
          $.getJSON();
  
          ajax.restore();
          sinon.assert.calledOnce(ajax);
          done();
      });
  });
  </code></pre>
  
  <p>I'm getting error like below during <code>mocha test</code></p>
  
  <pre><code>1) Tutor Test getJSON Calling ajax:
       TypeError: Attempted to wrap undefined property ajax as function
        at Object.wrapMethod (node_modules/sinon/lib/sinon/util/core.js:113:29)
        at Object.spy (node_modules/sinon/lib/sinon/spy.js:41:26)
        at Context.&lt;anonymous&gt; (test.js:41:26)
  </code></pre>