title: Creating request stub with sinon in mocha
body: <p>I'm using <code>mocha</code> to test some classes and I need to create a stub of <code>request</code> library. </p>
  
  <p>I'm using <code>sinon</code>,  and I'm able to create a stub of the <code>request.get</code> method but I'm not able to create a stub of the <code>request</code> method (the http calls try to connect to a server). As I have read, <code>request.get</code> is an alias for <code>request</code> but when I stub <code>request.get</code> it has no effect over <code>request</code> calls.</p>
  
  <p>This code works (using <code>request.get</code>):</p>
  
  <p>In tests:</p>
  
  <pre><code>request = require 'request'
  
  describe "User test", -&gt;
    user = {}
    before (done) -&gt;
      user = new test.user('Ander', 18)    
      sinon.stub(request, 'get').yields(null, {statusCode: 200}, 'foo')        
      done()
    after (done) -&gt;
      request.get.restore()
      done()
    it "testing server response", -&gt;
      user.getData().should.equal 'ander'
  </code></pre>
  
  <p>In source:</p>
  
  <pre><code>request = require 'request'
  
  class User
    contructor(@name, @age): -&gt;
    getData: -&gt;
      mydata = ''
      request.get 'http://127.0.0.1:8080/', (err, response, body) -&gt;        
        if not err and response.statusCode == 200
          mydata = body
        else
          err = throw new Error "Errorea"
      mydata
  </code></pre>
  
  <p>But this doesn't work (tries to connect to the supplied url):</p>
  
  <p>In tests:</p>
  
  <pre><code>request = require 'request'
  
  describe "User test", -&gt;
    user = {}
    before (done) -&gt;
      user = new test.user('Ander', 18)    
      sinon.stub(request, 'Request').yields(null, {statusCode: 200}, 'foo')
      #Creating the stub this way, doesn't work neither
      #sinon.stub(request, 'get').yields(null, {statusCode: 200}, 'foo')
      done()
    after (done) -&gt;
      request.Request.restore()
      done()
    it "testing server response", -&gt;
      user.getData().should.equal 'ander'
  </code></pre>
  
  <p>In source:</p>
  
  <pre><code>request = require 'request'
  
  class User
    contructor(@name, @age): -&gt;
    getData: -&gt;
      mydata = ''
      request 'http://127.0.0.1:8080/', (err, response, body) -&gt;        
        if not err and response.statusCode == 200
          mydata = body
        else
          err = throw new Error "Errorea"
      mydata
  </code></pre>
  
  <p>Which is the right way to create a stub for <code>request</code> call? Which is the method to be stubed? </p>