I'm currently trying to write a test using sinon and sinon-express-mock to mock an incorrect request and then call a validation function in my application, ensuring that the validation function returns the correct response status (400). However, currently I am getting the error TypeError: Cannot read property 'send' of undefined. I thought send would be mocked along with the rest of the res object, but if not how can I accomplish this? Thanks in advance.
The function I am testing:
export const validateItemRequest = (req, res) => {
if (!req.query.number) {
return res.status(400).send('Number not specified');
} else if ( req.query.number % 1 !== 0) {
return res.status(400).send('Incorrect number syntax');
}
};
The test code:
describe('item', function() {
it('should only accept valid requests', function() {
const itemRequest = {
query: {
number: 'abcde',
},
};
const req = mockReq(request);
const res = mockRes();
itemController.validateItemRequest(req, res);
});
});
sinon-express-mockcurrently does not support chaining. There's an open issue to add chaining support, but the methods currently don't return the object making it impossible to chain fromres.statustosend.I only needed a few of the response methods for my test, so I made my own response spy like this:
Notice I only return the response from
statussince you shouldn't chain offsendandsendStatus.