Testing for Express response locals

I am using Express.js 2.5.8. In an effort to reduce duplication, I am looking to use a dynamicHelper to pass commonly-used objects to the view without explicitly rendering them inside each route.

I have looked at the source for ways of intercepting locals on their way to the view, but without much success. I can potentially confirm their existence by checking on the app.dynamicViewHelpers object. However, I would like to know if there is a less implementation-dependent way of achieving this.

An ideal solution would be agnostic of how values and objects are being passed to the view. Whether they come from a viewHelper, middleware, or the route itself, the test should pass without modification. That is the ideal, anyway. I will settle for other approaches.

A loose example of what I'm looking to test:

app.dynamicHelpers({
  example : function(req, res){
    return "Example Value";
  }
});

app.get('/example', function(req, res){
  res.render('example-view', {
    sample : "Sample Value"
  });
});

// test that example === "Example Value" in the view
// test that sample === "Sample Value" in the view

This is a really great question. I suppose the best way to do this is by tapping into Express’ view system. If you are using Express 2, it could look like the following:

var express = require('express');
var app = express.createServer();

express.view.compile = function (view, cache, cid, options) {
  // This is where you get the options as passed to the view
  console.log(options);

  return {
    fn: function () {}
  };
};

app.locals({
  passed_via_locals: 'value'
});

app.get('/', function (req, res, next) {
  res.render('index', {
    passed_in_render: 'value',
    layout: false
  });
});

app.listen('./socket');

var http = require('http');

http.get({socketPath: './socket'});

In Express 3, this becomes much easier:

var express = require('express');
var app = new express();

function View() {
  this.path = true;
};
View.prototype.render = function(options, cb) {
  // This is where you get the options as passed to the view
  console.log(options);
};
app.set('view', View);

app.locals({
  passed_via_locals: 'value'
});

app.render('index', {
  passed_in_render: 'value'
});