Mock 'http' objects for testing Express routing functions
This project is available as a NPM package.
$ npm install node-mocks-http
After installing the package include the following in your test files:
var httpMocks = require'node-mocks-http';
Suppose you have the following Express route:
And you have created a function to handle that route's call:
You can easily test the
routeHandler function with some code like
this using the testing framework of your choice:
exports'routeHandler - Simple testing' =var request = httpMockscreateRequestmethod: 'GET'url: '/user/42'params:id: 42;var response = httpMockscreateResponse;routeHandlerrequest response;var data = JSONparse response_getData ;testequal"Bob Dog" dataname;testequal42 dataage;testequal"email@example.com" dataemail;testequal200 responsestatusCode ;testok response_isEndCalled;testok response_isJSON;testok response_isUTF8;testdone;;
We wanted some simple mocks without a large framework.
We also wanted the mocks to act like the original framework being mocked, but allow for setting of values before calling and inspecting of values after calling.
We are looking for more volunteers to bring value to this project, including the creation of more objects from the HTTP module.
This project doesn't address all features that must be mocked, but it is a good start. Feel free to send pull requests, and a member of the team will be timely in merging them.
After making any changes, please verify your works.
npm install -g jshint
Navigate to the project folder and run
npm install to install the
Then simply run the tests.
Most releases fix bugs with our mocks or add features similar to the
Response objects offered by Node.js and extended
Licensed under MIT.