尝试测试中间件的失败情况,现在v1TransformResponse将在单元测试中对某些验证抛出错误,我无法获得预期的结果,知道在下面的测试中实现了什么错误吗?我添加了我遇到的错误。
server.js
app.post('/cvs/v1/drugprice/:membershipId', orchestrateDrugPrice, v1TransformResponse);
v1TransformResponse.js
module.exports = async (req, res) => {
try {
const validateResponse = responseHandler(req.drugPriceResponse);
const transformedResponse = transformResponse(validateResponse);
const filterDrug = filteredResponse(transformedResponse);
logDrugPriceResponse('TRANSFORMED_RESPONSE V1', filterDrug);
res.status(200).send({ drugPrice: filterDrug });
} catch (error) {
if (error instanceof AppError) {
res.status(error.response.status).send(error.response.payload);
} else {
res.status(500).send(defaultErrorResponse);
}
}
};
main.test.js
const { expect } = require('chai');
const sinon = require('sinon');
const { spy, stub } = require('sinon');
const request = require('supertest');
const app = require('./../../../server/server');
const v1TransformResponse = require('./../../../server/middleware/v1TransformResponse');
const orchestrateDrugPrice = require('./../../../server/middleware/orchestrateDrugPrice');
describe('v1Transform()', () => {
let status,
send,
res;
beforeEach(() => {
status = stub();
send = spy();
res = { send, status };
status.returns(res);
});
describe('if called with a request that doesn\'t have an example query', () => {
const req = {
drugPriceResponse: [{
'brand': false,
'drugName': 'Acitretin',
'drugStrength': '10mg',
'drugForm': 'Capsule',
'retailPrice': {
'copayEmployer': '0',
'costAnnual': '3',
'costEmployer': '733.84',
'costToday': 'N/A',
'daysSupply': '30',
'deductible': 'n/a',
'memberCopayAmount': '30',
'NDC11': '378702093',
'penalties': 'N/A',
'totalDrugCost': '763.84'
}
}]
};
beforeEach(() => (req, res));
it('should return error if prices are ommitted', async () => {
try {
await v1TransformResponse(req, res);
} catch (error) {
expect(error.response).to.deep.equal({
httpStatus: 500,
payload: {
status: 500,
title: 'Internal Server Error',
detail: 'Drug prices are not valid'
}
});
}
});
});
});
错误:
if called with a request that doesn't have an example query
should return error if prices are ommitted:
AssertionError: expected undefined to deeply equal { Object (httpStatus, payload) }
答案 0 :(得分:1)
中间件v1TransformResponse
在失败的情况下不会引发错误。它调用res.status
方法。您需要检查传递给它的参数。
it('should return error if prices are ommitted', async () => {
await v1TransformResponse(req, res);
expect(res.status.getCall[0].args[0]).to.equal(500);
});