【问题标题】:how to test global variables with QUnit?如何使用 QUnit 测试全局变量?
【发布时间】:2018-01-23 15:32:57
【问题描述】:

我是 QUnit 新手,这是针对我正在开发的 SAPUI5 应用程序。

我的控制器中有以下代码

sap.ui.define( ["sap/ui/core/mvc/Controller", 
'BatchProcessing/model/ViewControls',
"sap/ui/core/routing/History",
'BatchProcessing/model/formatter',
'BatchProcessing/model/DataConnection',
'sap/m/MessageBox',
'sap/m/Dialog',
'sap/m/Button'

], function (Controller, ViewControls, History, formatter, dataConnector, 
MessageBox, Dialog, Button) {
"use strict";

var detailsControls;
var data = [];
var rowData;
return Controller.extend("BatchProcessing.controller.Detailsview", {

    onInit : function () {

        var route = sap.ui.core.UIComponent.getRouterFor(this);
        route.getRoute("page3").attachMatched(this.initController, this);   
    },

    initController: function(testObj){

        detailsControls = testObj || ViewControls.detailsViewControls.apply(this);

        detailsControls.evalPriceButton.setEnabled(true);
        detailsControls.awardQuantity.setEnabled(true);
        detailsControls.buyQuantity.setEnabled(true);
        detailsControls.priorUnitPrice.setEnabled(true);
        detailsControls.proposedUnitPrice.setEnabled(true);
        detailsControls.awardDate.setEnabled(true);
        detailsControls.proposedUnitPrice.setValue('');
        detailsControls.buyQuantity.setValue('');
        detailsControls.priorUnitPrice.setValue('');
        detailsControls.awardQuantity.setValue('');
        detailsControls.awardDate.setValue('');
        detailsControls.BZZPIIN = '';
        detailsControls.PRC = '';
        detailsControls.VENDOR = '';
        detailsControls.CAGE = '';
        detailsControls.ORDER_QUAN = '';
        detailsControls.NETPRICE = '';
        detailsControls.acceptBtn.setEnabled(false);

        //retrieve data selected by user on previous page
        rowData = ViewControls.getRowData();

        ViewControls.setPriceChangeAccepted(false);//set changes accepted to 'Not Accepted'

        data = dataConnector.getContractHistoryData(rowData[0].M_BIC_B_NSN); 

        this.setPriceModel(rowData);
        this.setPaginationData();
    },

这是我对上述代码的 QUnit 测试。我只是在尝试设置它。

 QUnit.test("initController setup", function(assert) {
        // Arrange 

        var setEnabled = sinon.spy();
        var setValue = sinon.spy();
        var getValue = sinon.spy();
        var setVisible = sinon.spy();
        var setVisibleRowCount = sinon.spy();
        var setValueState = sinon.spy();
        var getRowData = sinon.stub().returns([
                                        {BAWAEFFDT: "20150213",
                                        BZZPIIN: "SPE5E967V1599",
                                        CAGE: "4R840",
                                        CUR_QTY: "20",
                                        FAIR_MKT_PRICE: "160.00",
                                        INDEX: "3",
                                        M_BIC_B_NSN: "306018040",
                                        NETPRICE: "150.99",
                                        ORDER_QUAN: "35",
                                        PERC_DIFF: "20",
                                        PROPOSED_PRICE: "25.99",
                                        PriceReasonableCode: "BG",
                                        VENDOR: "STAMCO."
                                    }]);

        var rowData = [
                        {BAWAEFFDT: "20150213",
                        BZZPIIN: "SPE5E967V1599",
                        CAGE: "4R840",
                        CUR_QTY: "20",
                        FAIR_MKT_PRICE: "160.00",
                        INDEX: "3",
                        M_BIC_B_NSN: "306018040",
                        NETPRICE: "150.99",
                        ORDER_QUAN: "35",
                        PERC_DIFF: "20",
                        PROPOSED_PRICE: "25.99",
                        PriceReasonableCode: "BG",
                        VENDOR: "STAMCO."
                    }];

        var detailsControls = {

                summaryTable:{
                    setVisibleRowCount: setVisibleRowCount
                },
                evalPriceButton: {
                    setEnabled: setEnabled
                },
                nextBtn:{
                    setVisible: setVisible,
                    setEnabled: setEnabled
                },
                prevBtn:{
                     setVisible: setVisible,
                     setEnabled: setEnabled
                },
                dropDown:{
                    getValue: getValue,
                    setValue: setValue

                },
                acceptBtn:{
                    setEnabled: setEnabled
                },
                awardQuantity:{
                    setValue: setValue,
                    getValue: getValue,
                    setEnabled: setEnabled,
                    setValueState: setValueState
                },
                missingFieldError:{
                    setVisible: setVisible
                },
                invalidFieldError: {
                    setVisible: setVisible
                },
                invalidDateError: {
                    setVisible: setVisible
                },
                buyQuantity: {
                    setEnabled: setEnabled,
                    setValue: setValue,
                    setValueState: setValueState
                },
                priorUnitPrice:{
                    setEnabled: setEnabled,
                    setValue: setValue,
                    setValueState: setValueState
                },
                proposedUnitPrice:{
                    setEnabled: setEnabled,
                    setValue: setValue,
                    setValueState: setValueState
                },
                awardDate:{
                    setEnabled: setEnabled,
                    setValue: setValue,
                    setValueState: setValueState
                }  
            };


        detailsController.initController(detailsControls);

    });  

我遇到的问题是,当我到达下一行时,我得到一个未定义的错误(rowData[0] 未定义)。

data = dataConnector.getContractHistoryData(rowData[0].M_BIC_B_NSN); 

我在函数中添加了以下几行,以使用 detailsControls 解决此问题。我通过 testObj 的唯一目的是让 QUnit 工作,或者我被告知要这样做。我不认为这是正确的,我不想为我的函数添加更多参数,这样我就可以让我的测试工作。我还没有找到类似的例子。谁能告诉我一种使这项工作正确的方法吗?

initController: function(testObj){

    detailsControls = testObj || 
   ViewControls.detailsViewControls.apply(this);

【问题讨论】:

    标签: javascript unit-testing sapui5 qunit


    【解决方案1】:

    只需存根返回数据的函数即可:

    sinon.stub(BatchProcessing.model.ViewControls, 'detailsViewControls').returns(detailsControls)
    

    【讨论】:

    • 我不认为我真的在关注你。我试过了,得到一个错误,说 BatchProcessing.model 是未定义的。为什么我要存根返回数据的函数?问题在于接收数据的变量 detailsControls。如何存根该变量?
    • @polaris,你不能存根变量,这就是为什么有必要存根一个返回数据并将这些数据分配给该变量的函数。 undefined 的问题可能是由于测试文件中“BatchProcessing/model/ViewControls”的错误依赖。
    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多