Node.js 如何在nodejs中使用sinon进行中间件测试?

Node.js 如何在nodejs中使用sinon进行中间件测试?,node.js,unit-testing,chai,sinon,Node.js,Unit Testing,Chai,Sinon,尝试测试中间件的失败场景时,v1TransformResponse将在单元测试中的某些验证上抛出错误。我无法获得预期结果,知道下面的测试中实现了什么错误吗?我已经添加了我得到的错误 server.js app.post('/cvs/v1/drugprice/:membershipId', orchestrateDrugPrice, v1TransformResponse); v1TransformResponse.js module.exports = async (req, res) =&g

尝试测试中间件的失败场景时,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) }

中间件
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);
});

如果出现故障,我将如何测试中间件返回的对象?