Testing Express.js res.render within promise with Mocha & Sinon spy
<p>Following a similar pattern to <a href="http://www.designsuperbuild.com/blog/unit_testing_controllers_in_express/" rel="nofollow">this example</a> I've been attempting to test my routes in an Express.js application but I'm unable to get my spy to verify that <code>res.render</code> has been called when wrapped in a <code>promise.then</code>.</p>

<p>Here's a simplified example where'd I expect <code>calledOnce</code> to be true but it's returning false.</p>

<p>Code under test:</p>

<pre><code>var model = {
  get: function() {
    return new Promise(function(res, rej) {
      return res('success');
    });
  }
};

module.exports = function (req, res) {
  model.get().then(function (data) {
    res.render('home', data);
  }).catch(function (err) {
    console.log(err);
  });
};
</code></pre>

<p>Test:</p>

<pre><code>var expect = require('chai').expect;
var sinon = require('sinon');
var home = require('./home');

describe('home route', function() {
  it('should return a rendered response', function() {
    var req = {};

    var res = {
      render: sinon.spy()
    };

    home(req, res);

    expect(res.render.calledOnce).to.be.true;
  });
});
</code></pre>