如何使用QUnit测试全局变量?

时间:2018-01-23 15:32:57

标签: javascript unit-testing sapui5 qunit

我是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);

1 个答案:

答案 0 :(得分:0)

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

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