Skip to content

Instantly share code, notes, and snippets.

@awinder
Last active June 16, 2016 13:49
Show Gist options
  • Save awinder/7508239 to your computer and use it in GitHub Desktop.
Save awinder/7508239 to your computer and use it in GitHub Desktop.
Mocking the Express.js response object
module.exports = {
test : function(req, res) {
setTimeout(function() {
res.send({ testing : true }, 200);
}, 5000);
},
}
var express = require('express')
, app = exports = module.exports = express()
, ctrl = require('./controller');
app.get('/', ctrl.test);
var util = require('util')
, events = require('events').EventEmitter;
var res = function () {
};
util.inherits(res, events);
res.prototype.send = function(payload, code) {
this.emit('response', {
code: code,
response: payload
});
}
module.exports = function() {
return new res();
};
var expect = require('chai').expect
, res = require('./mocks/response')()
, testing = require('./controller');
describe('Testing', function(){
it('Should send an object with a testing key', function(done){
res.on('response', function(resp) {
expect(resp.response).to.have.property('testing');
expect(resp.response.testing).to.equal(true);
done();
});
testing.test({}, res);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment