title: Sinon - how to stub nested function?
body: <p>Apologies if this is a simple question, I'm relatively new to Node and Sinon.  I'm struggling trying to figure out how to assert that a nested asynchronous function was called in Nodejs.  </p>
  
  <p>I'm using mocha, chai, sinon, and request (<a href="https://github.com/request/request" rel="noreferrer">https://github.com/request/request</a>)  but think I'm missing something basic on the stubbing part.  </p>
  
  <p>Example inside my_app.js - </p>
  
  <pre><code>var request = require('request');
  
  function MyModule() {
  };
  
  MyModule.prototype.getTicker = function(callback) {
      request('http://example.com/api/ticker', function(error, response) {
          if (error) {
              callback(error);
          } else {
              callback(null, response);
          }
      });
  };
  
  exports.mymodule = new MyModule();
  </code></pre>
  
  <p>Inside the test.  I'm trying to stub out the call to request and provide some dummy data to return.  But I keep getting an error "request is not defined" on the line where I"m creating the stub.  </p>
  
  <pre><code>var myApp = require('../my_app.js')
      ,assert = require("assert")
      ,chai = require('chai')
      ,sinon = require('sinon')
      ,expect = chai.expect;
  
  describe('mymodule object', function() {
  
      var mymodule = myApp.mymodule;
  
      before(function(done) {
          sinon.stub(request).yields(null, JSON.stringify({
              price: '100 USD'
          }));
          done();
      });
  
      it('getTicker function should call request on example ticker', function(done) {
          mymodule.getTicker(function(error, result){
              request.called.should.be.equal(true);
              done();
          });
      });
  
  });
  </code></pre>
  
  <p>I know I can assign sinon.stub(objname, "funcname") or sinon.stub("funcname"), but those only set the  outer object , I'm trying to stub the function request which is inside the function getTicker.  </p>
  
  <p>Any ideas on how to do this?  Maybe I need to use a spy as well (but how?)  Or is there a better approach to test the above getTicker function?</p>