简体   繁体   English

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

[英]how to test global variables with QUnit?

I'm new to QUnit and this is for an SAPUI5 application I'm working on. 我是QUnit的新手,这是我正在使用的SAPUI5应用程序。

I have the following code in my controller 我的控制器中有以下代码

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();
    },

This is my QUnit test for the above code. 这是我针对上述代码的QUnit测试。 I'm just trying to set it up. 我只是想设置它。

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

    });  

The problem I have is that when I get to the following line, I get an undefine error (rowData[0] is undefined). 我遇到的问题是,当我转到下一行时,收到一个未定义的错误(rowData [0]未定义)。

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

I added the following lines to my function to solve this problem with detailsControls. 我在函数中添加了以下几行,以通过detailsControls解决此问题。 I'm passing testObj for the sole purpose of making QUnit work, or so was I told to do. 我通过testObj的唯一目的是使QUnit工作,或者被告知这样做。 I don't think this is correct and I don't want to add more parameters to my functions just so I can make my tests work. 我认为这是不正确的,并且我不想在功能中添加更多参数,只是为了使测试正常进行。 I haven't found an example similar to this. 我没有找到类似的例子。 Could anyone show me a way of making this work the correct way? 谁能告诉我一种使这项工作正确无误的方法?

initController: function(testObj){

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

只需对返回数据的函数进行存根:

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

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

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