title: Stubbing a constructor of a third party library when testing express app with Supertest
body: <p>I've got a simple express app that looks like this:</p>
  
  <pre><code>var SendMandrillTemplate = require('send-mandrill-template');
  var sendMandrillTemplate = new SendMandrillTemplate('api-key-goes-here');
  
  var app = require('express')();
  
  app.get('/', function(req, res, next) {
  
      sendMandrillTemplate.sendTemplate(
          'template-name-goes-here',
          'email@here.com', {
              value: 123
          },
          function(err) {
              if (err) {
                  res.send('ERR - ', err)
              } else
                  res.send('DONE')
          });
  });
  
  module.exports = app;
  </code></pre>
  
  <p>I export the app object, so I can mount this in a separate <code>server.js</code> like this -</p>
  
  <pre><code>var app = require('./app')
  
  app.listen(1234, function() {
      console.log('Running on port 1234');
  });
  </code></pre>
  
  <p>This is to enable me to use supertest a bit easier.</p>
  
  <p>Here's my test so far:</p>
  
  <pre><code>var app = require('./app')
  var request = require('supertest')
  
  var SendMandrillTemplate = require('send-mandrill-template');
  
  describe('GET /', function() {
  
      var sendTemplateStub;
      before(function() {
          //I think i need to setup a spy on the created instance of SendMandrillTemplate.sendTemplate
          //sendTemplateStub = //?
      });
  
      it('calls sendTemplate on sendMandrillTemplate instance', function(done) {
          request(app)
              .get('/')
              .expect(200)
              .end(function(err, res) {
                  if (err) throw err;
  
                  //assert sendTemplateStub was called with 'template-name-goes-here'
                  //etc...
  
                  done();
              })
      })
  })
  </code></pre>
  
  <p>As you can see, I'm having trouble stubbing the SendMandrillTemplate constructor</p>
  
  <p>If I wasn't newing up an instance of <code>SendMandrillTemplate</code> I could do something like:</p>
  
  <pre><code>sendTemplateStub = sinon.stub(SendMandrillTemplate, 'sendTemplate')
  </code></pre>
  
  <p>But of course, in this scenario this won't work...</p>