Sapui5 如何在ui5中创建存根视图模型?

Sapui5 如何在ui5中创建存根视图模型?,sapui5,sinon,qunit,Sapui5,Sinon,Qunit,我是qunit+sinon.js的新手,我想在MultiselectPress上为函数编写一个单元测试,所以我需要模拟: this.myController.\u oList this.myController.getResourceBundle() this.myController.getModel(“masterView”) 对吧? 我一直在为getModel(“masterView”)获取存根,有什么建议吗 onInit : function () { var oList = t

我是qunit+sinon.js的新手,我想在MultiselectPress上为函数
编写一个单元测试,所以我需要模拟:

this.myController.\u oList

this.myController.getResourceBundle()

this.myController.getModel(“masterView”)

对吧?

我一直在为
getModel(“masterView”)
获取存根,有什么建议吗

onInit : function () {
    var oList = this.byId("list"),
        oViewModel = this._createViewModel();   
    this._oList = oList;
    this.setModel(oViewModel, "masterView");    
},

_createViewModel : function() {
    return new JSONModel({
        isFilterBarVisible: false,
        filterBarLabel: "",
        delay: 0,
        title: this.getResourceBundle().getText("masterTitleCount", [0]),
        noDataText: this.getResourceBundle().getText("masterListNoDataText"),
        sortBy: "Name",
        groupBy: "None",
        listMode: "SingleSelectMaster",
        showDeleteButton: false
    });
},

getModel : function (sName) {
    return this.getView().getModel(sName);
},

onMultiSelectPress : function () {
    var oMasterViewModel = this.getModel("masterView");

    switch(this._oList.getMode()) {
        case "MultiSelect":
            oMasterViewModel.setProperty("/listMode", "SingleSelectMaster");
            oMasterViewModel.setProperty("/showDeleteButton", false);
            break;
        case "SingleSelectMaster":
            oMasterViewModel.setProperty("/listMode", "MultiSelect");
            oMasterViewModel.setProperty("/showDeleteButton", true);
            break;
    }
},

在每个
之前的
中添加一个OviewTub,并使用设置一个空JSON模型进行测试

    QUnit.module("MasterController", {
        beforeEach: function() {
            this.oMasterController = new MasterController();
            this.models = {};
            var oViewStub = {
                setModel: function(model, name) {
                    this.models[name] = model;
                }.bind(this),
                getModel: function(name) {
                    return this.models[name];
                }.bind(this)
            };
            sinon.stub(Controller.prototype, "getView").returns(oViewStub);
        },

        afterEach: function() {
            this.oMasterController.destroy();
            jQuery.each(this.models, function(i, model) {
                model.destroy();
            });
            Controller.prototype.getView.restore();
        }
    });

    QUnit.test("test onMultiSelectPress() ", function(assert) {
        var oMasterController = this.oMasterController;
        var oModel = new JSONModel();
        oMasterController.setModel(oModel, "masterView");
        var oMasterViewModel = oMasterController.getModel("masterView");

        oMasterController._oList = new sap.m.List();
        sinon.stub(oMasterController._oList, "getMode").returns("MultiSelect");
        oMasterController.onMultiSelectPress();
        assert.strictEqual(oMasterViewModel.getProperty("/listMode"), "SingleSelectMaster", "Did change list mode to SingleSelectMaster");
        assert.strictEqual(oMasterViewModel.getProperty("/showDeleteButton"), false, "Did hide the delete button");

        oMasterController._oList.getMode.restore();
        sinon.stub(oMasterController._oList, "getMode").returns("SingleSelectMaster");
        oMasterController.onMultiSelectPress();
        assert.strictEqual(oMasterViewModel.getProperty("/listMode"), "MultiSelect", "Did change list mode to MultiSelect");
        assert.strictEqual(oMasterViewModel.getProperty("/showDeleteButton"), true, "Did show the delete button");

        oMasterController._oList.destroy();
    });

在每个
之前的
中添加一个OviewTub,并使用设置一个空JSON模型进行测试

    QUnit.module("MasterController", {
        beforeEach: function() {
            this.oMasterController = new MasterController();
            this.models = {};
            var oViewStub = {
                setModel: function(model, name) {
                    this.models[name] = model;
                }.bind(this),
                getModel: function(name) {
                    return this.models[name];
                }.bind(this)
            };
            sinon.stub(Controller.prototype, "getView").returns(oViewStub);
        },

        afterEach: function() {
            this.oMasterController.destroy();
            jQuery.each(this.models, function(i, model) {
                model.destroy();
            });
            Controller.prototype.getView.restore();
        }
    });

    QUnit.test("test onMultiSelectPress() ", function(assert) {
        var oMasterController = this.oMasterController;
        var oModel = new JSONModel();
        oMasterController.setModel(oModel, "masterView");
        var oMasterViewModel = oMasterController.getModel("masterView");

        oMasterController._oList = new sap.m.List();
        sinon.stub(oMasterController._oList, "getMode").returns("MultiSelect");
        oMasterController.onMultiSelectPress();
        assert.strictEqual(oMasterViewModel.getProperty("/listMode"), "SingleSelectMaster", "Did change list mode to SingleSelectMaster");
        assert.strictEqual(oMasterViewModel.getProperty("/showDeleteButton"), false, "Did hide the delete button");

        oMasterController._oList.getMode.restore();
        sinon.stub(oMasterController._oList, "getMode").returns("SingleSelectMaster");
        oMasterController.onMultiSelectPress();
        assert.strictEqual(oMasterViewModel.getProperty("/listMode"), "MultiSelect", "Did change list mode to MultiSelect");
        assert.strictEqual(oMasterViewModel.getProperty("/showDeleteButton"), true, "Did show the delete button");

        oMasterController._oList.destroy();
    });

我怎样才能对onInit方法进行单元测试?我可以测试方法的存在性,但无法在其中测试JSON
onInit:function onInit(){//HTML字符串绑定到格式化文本控件var oModel=new JSONModel({title:'Hello',message:'You

'+接受:'I accept',拒绝:'I denced'});this.getView().setModel(oModel);},
断言.ok(this.getView().getModel(oModel))
@user557657在单元测试中,我无法获取
这个
引用。@user557657,我的错误。对于
this.oMasterController=new MasterController()
,请尝试注销此.oMasterController.getView().getModel(oModel)
如何在init方法上进行单元测试?我可以测试方法的存在性,但无法在其中测试JSON
onInit:function onInit(){//HTML字符串绑定到格式化文本控件var oModel=new JSONModel({title:'Hello',message:'You

'+接受:'I accept',拒绝:'I denced'});this.getView().setModel(oModel);},
断言.ok(this.getView().getModel(oModel))
@user557657在单元测试中,我无法获取
这个
引用。@user557657,我的错误。对于
this.oMasterController=new MasterController()
,尝试注销此.oMasterController.getView().getModel(oModel)