繁体   English   中英

Angular中jQuery数据表的回调方法单元测试6

[英]Unit test of jQuery datatable's callback method in Angular 6

无论如何使用业力/茉莉花测试 Angular 6 中 jQuery 数据表的回调方法?

我想测试以下方法的页脚回调,但我想不出任何方法。 我是单元测试的新手,在 web 上找不到任何有用的东西。

 initializeBugIndexPageSetting() {
    let self = this;
    $('example').DataTable({
        'footerCallback': function (row, data, start, end, display) {
            const api = this.api();
            // Remove the formatting to get integer data for summation
            const intVal = function (i) {
                if (i === 'N/A') {
                    return 0;
                }
                return i * 1;
            };
            // Total over this page
            const pageInProgrssToResolved = api
                .column(14, { page: 'current' })
                .data()
                .reduce(function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0);
            const pageCount = api
                .column(5, { page: 'current' })
                .data().count();
           // Update footer           
            $(api.column(14).footer()).html((pageInProgrssToResolved / pageCount).toFixed(2));
        }
    });
}

一点帮助和指导。

我建议将回调的内容放在一个单独的方法中,因为这个新的 function 非常容易进行单元测试。

And as a second test I would suggest to test the initializeBugIndexPageSetting function by adding a jasmine spy on the new function that is then called in the footerCallback and you can just check if it has been called (.toHaveBeenCalledTimes(1) on the spy after calling初始化BugIndexPageSetting 函数)。

您可以在这里找到有价值的教程: https://scriptverse.academy/tutorials/jasmine-createspy-createspyobj.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM