微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的单元测试说一个页面没有被渲染,而它显然已经被渲染了?

如何解决为什么我的单元测试说一个页面没有被渲染,而它显然已经被渲染了?

这是有问题的单元测试:

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 调用。当我运行测试时,它会弹出,所以我知道它到达(并可能执行)渲染。

有什么建议吗?

解决方法

将以下行移动到异步函数:

 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 指路。)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?