title: Use SinonJS to stub and spy on the same function?
body: <p>In the following example, I want to <a href="http://sinonjs.org/docs/#stubs" rel="noreferrer">stub</a> the <code>get</code> function to prevent the actual HTTP request from occurring.  I want to <a href="http://sinonjs.org/docs/#spies" rel="noreferrer">spy on</a> the <code>get</code> method to check what arguments it was called with.</p>
  
  <pre><code>var request = require('request'), sinon = require('sinon');
  describe('my-lib', function() {
    sinon.stub(request, 'get').yield(null, null, "{}");
    var spy = sinon.spy(request, 'get');
    it('should GET some data', function(done) {
      function_under_test(function(err, response) {
        if(error) return done(error);
        assert(request.get.called);
        assert(request.get.calledWith('some', 'expected', 'args'));
      });
    });
  });
  </code></pre>
  
  <p>Sinon does not seem to allow spying and stubbing the same method, though.  The above example gives the following error:</p>
  
  <pre><code>TypeError: Attempted to wrap get which is already wrapped
  </code></pre>
  
  <hr>
  
  <p>How do I spy on a method, while preventing default behaviour?</p>