Last active
June 16, 2016 13:49
-
-
Save awinder/7508239 to your computer and use it in GitHub Desktop.
Mocking the Express.js response object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
test : function(req, res) { | |
setTimeout(function() { | |
res.send({ testing : true }, 200); | |
}, 5000); | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
, app = exports = module.exports = express() | |
, ctrl = require('./controller'); | |
app.get('/', ctrl.test); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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