Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add end, send and render events for async testing. #11

Merged
merged 2 commits into from
Mar 30, 2014

Conversation

invernizzie
Copy link
Contributor

This closes #6. We can write tests for asyncrhonous responses as shown below.

Code under test:

function myHandler(request, response) {
  setTimeout(function() {
    response.end();
  }, 1000);
}
exports['End is called'] = function (test) {
  request = httpMocks.createRequest();
  var response = httpMocks.createResponse({
    eventEmitter: EventEmitter
  });

  response.on('end', function () {
    test.ok(response._isEndCalled());
    test.done();
  });

  myHandler(request, response);
};

@tobyhinloopen
Copy link

Could someone please accept this? Very thanks.

howardabrams added a commit that referenced this pull request Mar 30, 2014
Add end, send and render events for async testing.
@howardabrams howardabrams merged commit c768b88 into eugef:master Mar 30, 2014
@knowmad
Copy link

knowmad commented Jan 10, 2015

Thanks for this code. It looks like it will solve my, as well as others', problems with async responses. Unfortunately, I'm running into a problem getting this to work. When I use the code as shown in the example, I get the following error -- "ReferenceError: EventEmitter is not defined."

If I then define EventEmitter, I can never seem to get an event handler to call my .on function. I even tried putting the emit call directly into my test and it still didn't work. I am running the test under Mocha if that makes any difference. Thanks for any help.

@knowmad knowmad mentioned this pull request Jan 10, 2015
@knowmad
Copy link

knowmad commented Jan 10, 2015

Figured it out. Here's what is working for me:

var events = require('events');
var ee = events.EventEmitter;
var response = httpMocks.createResponse({
eventEmitter: ee
});

@howardabrams
Copy link
Collaborator

Would creating a mock version of EventEmitter be sufficient? Could you give me a test case you would like to see, and we can try to come up with something?

@knowmad
Copy link

knowmad commented Jan 11, 2015

It looks like you are loading a mock emitter in the mockResponse.js at line 29. The mock emitter was not working for me when I tried to use it by not sending any eventEmitter option when calling createResponse(). My test code is working when I used the version above.

The main thing we need is to be able to have our tests wait while the request is processed. Due to node's async style, that was not happening when the tests were placed immediately after calling the route handler as shown in your sample. I suspect that's due to our making database calls as part of processing the request.

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'end' or 'close' event or callback for response
4 participants