【问题标题】:How to stub view model in ui5?如何在ui5中存根视图模型?
【发布时间】:2018-03-27 13:59:36
【问题描述】:

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

this.myController._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;
    }
},

【问题讨论】:

    标签: sapui5 sinon qunit


    【解决方案1】:

    beforeEach中添加一个oViewStub,并设置一个空的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 string bound to the formatted text control var oModel = new JSONModel({ title: 'Hello', message: '<p>You </p>' + accept: 'I Accept', decline: 'I Decline' }); this.getView().setModel(oModel); },
    • assert.ok(this.getView().getModel(oModel)) ? @user557657
    • 我无法在单元测试中获得this 引用。
    • @user557657,我的错。对于this.oMasterController = new MasterController();,请尝试注销this.oMasterController.getView().getModel(oModel)
    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 2016-07-18
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多