eugef / node-mocks-http

Mock 'http' objects for testing Express routing functions
Other
755 stars 134 forks source link

Documentation for use of Express Response #148

Closed tylrob closed 4 years ago

tylrob commented 6 years ago

Hi team, great project here. I found the repo by searching for how to unit test res.locals, and I see there was some activity to support this in the following commit: https://github.com/howardabrams/node-mocks-http/commit/e828c48af417384bdeaad9fe975e7ee091304b66

Can anyone show documentation about how to use the Express Response versus the regular response?

tylrob commented 6 years ago

This is what I ended up going with, based on test/lib/mockExpressResponse.spec.js, but I wonder if this is the preferred approach.

const mockExpressResponse = require('node-mocks-http/lib/express/mock-express').response
const mockRequest = require('node-mocks-http/lib/mockRequest')

/*my middleware to test*/
let RegistrationController = require('../app/controllers/registration.js')

describe('getRegistration', function () {

  it('should give status 200', function (done) {
    let req = mockRequest.createRequest()
    let res = mockExpressResponse.createResponse()
    let next = () => {} //we don't care about this since we just want to mock the passed flow

    RegistrationController.getRegistration(req, res, next)

    assert.equal(res.statusCode, 200)
    done()
  })
github-actions[bot] commented 4 years ago

Stale issue message