这是有问题的单元测试:
const chai = require('chai');
const sinonChai = require('sinon-chai');
const sinon = require('sinon');
const appRefAPI = require('../../../../../../app/services/api');
const { getMarkAsManual, submitMarkAsManual } = require('../../../../../../app/services/handler/manual/mark-as-manual-handler');
const appRefResult = JSON.parse(JSON.stringify(require('../../../response/application-received-full')));
const postData = JSON.parse(JSON.stringify(require('../../../response/post-app')));
const offerAccepted = JSON.parse(JSON.stringify(require('../../../response/exception-offer-accepted')));
const { expect } = chai;
chai.use(sinonChai);
describe('details/mark-as-manual-handler.js', () => {
let req;
let res;
let sandbox;
describe.only('submitMarkAsManual()', async () => {
before(() => {
res = {
render: () => ({})
};
req = {
session: {}
};
sandbox = sinon.createSandbox();
});
beforeEach(() => {
sandbox.stub(res, 'render').returns({});
sandbox.stub(appRefAPI, 'postClose').returns([200, postData]);
});
afterEach(() => {
sandbox.restore();
});
it('should render-manually-process-confirmation', () => {
req.session.application_reference = 'EZ123456';
req.session.data = offerAccepted
req.body = {
'manually-processed-day': '3',
'manually-processed-month': '3',
'manually-processed-year': '1999'
}
res.locals = {};
res.locals.application_reference = req.session.application_reference;
submitMarkAsManual(req, res);
console.log(res.render)
expect(res.render).to.have.been.calledOnceWith('pages/manually-process-confirmation');
});
});
});
这是它正在查看的代码:
const submitMarkAsManual = async (req, res) => {
const errors = [];
let dd = req.body['manually-processed-day'];
let mm = req.body['manually-processed-month'];
let yyyy = req.body['manually-processed-year'];
if (dd.length === 1) dd = '0'+dd;
if (mm.length === 1) mm = '0'+mm;
const credit_date = `${dd}/${mm}/${yyyy}`
res.locals = req.session.data;
res.locals.credit_date = credit_date;
if (util.isValidDate(credit_date) === false) {
errors.push('date-invalid');
res.render('pages/mark-as-manual', { errors });
} else {
let data = {
"application_id": req.session.application_reference,
"closure_reason": "offer_response_processed_manually",
"credit_date": credit_date
}
const response = await callAPI.postClose(data);
if (response[0] === 200 && response[1].status === 'SUCCESS') {
console.log('success!!!!')
res.render('pages/manually-process-confirmation');
}else{
res.redirect('/budgeting-loans-ui/problem-with-service');
}
}
};
从中我得到了以下信息:
1) details/mark-as-manual-handler.js
submitMarkAsManual()
should render-manually-process-confirmation:
AssertionError: expected render to have been called exactly once with arguments pages/manually-process-confirmation
at Context.<anonymous> (test/unit/app/services/handler/manual/mark-as-manual-handler-test.js:85:45)
at processImmediate (node:internal/timers:463:21)
在正在测试的代码中,就在渲染之前,我放入了一个console.log调用,该调用输出"success!!!"。当我运行测试时,它会弹出,这样我就知道它到达(并可能执行(渲染。
有什么建议吗?
将以下行移动到异步函数:
submitMarkAsManual(req, res);
console.log(res.render)
expect(res.render).to.have.been.calledOnceWith('pages/manually-process-confirmation');
调用异步函数:
const sub = async (req, res) => {
await submitMarkAsManual(req, res)
expect(res.render).to.have.been.calledOnceWith('pages/manually-process-confirmation');
}
(感谢IAmDranged为您指路。(