Skip to content

Instantly share code, notes, and snippets.

@der-On
Last active December 27, 2015 17:29
Show Gist options
  • Save der-On/7362699 to your computer and use it in GitHub Desktop.
Save der-On/7362699 to your computer and use it in GitHub Desktop.
possible geddy controller stubing
var EventEmitter = require('events').EventEmitter;
var util = require('utilities');
var MockRequest = function () {
this.headers = {
accept: '*/*'
}
};
module.exports.MockRequest = MockRequest;
function addFlashStubToController(controller) {
controller.flash = {
set:function (type, msg) {
controller.flashMessage = {type:type,msg:msg};
},
alert: function(msg) { controller.flash.set('alert', msg); },
info: function(msg) { controller.flash.set('info', msg); },
success: function(msg) { controller.flash.set('success', msg); },
error: function(msg) { controller.flash.set('error', msg); },
keep: function() {},
discard: function() {}
}
}
module.exports.addFlashStubToController = addFlashStubToController;
function addSessionStubToController(controller)
{
controller.session = {
id: '123456789',
data: {},
set: function(name, value) {
controller.session.data[name] = value;
},
get: function(name) {
return controller.session.data[name] || null;
},
close: function() {}
}
}
module.exports.addSessionStubToController = addSessionStubToController;
function addOutputStubToController(controller)
{
util.enhance(controller, new EventEmitter());
controller.output = function (statusCode, headers, content) {
controller.emit('output', statusCode, headers, content);
};
}
module.exports.addOutputStubToController = addOutputStubToController;
function addStubsToController(controller) {
controller.params = {};
controller.request = new MockRequest();
addFlashStubToController(controller);
addSessionStubToController(controller);
addOutputStubToController(controller);
controller.canRespondTo(['html', 'json', 'js']);
controller.renderTemplate = function (data, opts, callback) {
callback('<div>' + JSON.stringify(data) + '</div>');
};
}
module.exports.addStubsToController = addStubsToController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment