New BP: test middlewares in isolation

This commit is contained in:
Yoni
2020-02-26 20:34:24 +02:00
parent 7424f0cf99
commit b8e7cc99fe
2 changed files with 9 additions and 9 deletions

View File

@ -543,7 +543,7 @@ All statements above will return false if used with `===`
**Otherwise:** A bug in Express middleware === a bug in all or most requests
🔗 [**Read More: Choosing CI platform**](/sections/testingandquality/test-middlewares.md)
🔗 [**Read More: Test middlewares in isolation**](/sections/testingandquality/test-middlewares.md)
<br/><br/><br/>

View File

@ -8,23 +8,23 @@ Many avoid Middleware testing because they represent a small portion of the syst
<br/><br/>
### Code example: Testing middleware in isolation without issuing network calls and waking-up the entire web framework
### Code example: Testing middleware in isolation
```javascript
//the middleware we want to test
const unitUnderTest = require('./middleware')
const httpMocks = require('node-mocks-http');
const unitUnderTest = require("./middleware");
const httpMocks = require("node-mocks-http");
//Jest syntax, equivelant to describe() & it() in Mocha
test('A request without authentication header, should return http status 403', () => {
test("A request without authentication header, should return http status 403", () => {
const request = httpMocks.createRequest({
method: 'GET',
url: '/user/42',
method: "GET",
url: "/user/42",
headers: {
authentication: ''
authentication: ""
}
});
const response = httpMocks.createResponse();
unitUnderTest(request, response);
expect(response.statusCode).toBe(403);
});
```
```