0
votes

I'm filddling with sinonjs in SAPUI5. But there are some things I can't get my head around.

        QUnit.module("Validation of Betaalwijze", {
        beforeEach : function () {
            this.oMainViewController = new MainViewController();
            this.oViewStub = new ManagedObject();

            var data = {
            IBANPrimair: "123",
            IBANSecundair: "456",
            Betaalwijze: ""
            };
            var oModel = new JSONModel(data);

            var fakeBetaalwijzeField = new Input();

            sinon.stub(this.oViewStub, "getModel").returns(oModel);
            sinon.stub(this.oViewStub, "byId").returns(fakeBetaalwijzeField); 
            sinon.stub(this.oMainViewController, "getView").returns(this.oViewStub);
        },

        afterEach : function() {
            this.oMainViewController.destroy();
            this.oViewStub.destroy();
            this.fakeBetaalwijzeField.destroy();
        }   
    });

    QUnit.test("Should set an ValueState Error", function (assert) {
        // Arrange
        //All preparation here above.


        // Act
        this.oMainViewController._validateInput();

        // Assert
        //TODO
    });

The getModel-stub works nicely when I use a "sap/ui/base/ManagedObject" for the oViewStub. But the byId-stub causes the message "Attempted to wrap undefined property byId as function" in that case. When I use a "sap/ui/core/mvc/View" for the oViewStub, the getModel-stub is not found. (But this gives an error in the beforeEach also: Cannot read property 'viewData' of undefined.)

What is the right way to stub the View and it's methods getModel() and byId()?

1
Should not be any problem with it. Make sure this.oViewStub object is sap.ui.core.mvc.View - Skay
Is your viewStub an actual sap.ui.core.mvc.View? Maybe provide a little more code esp. creation of viewStub. Furthermore I would recommend using sandboxes: sinonjs.org/releases/v1.17.6/sandbox - cschuff
Alright, the next step is using sandboxes, noted. For now i simply use destroy. I tried your suggestion @cschuff. But I must be overseeing something bigtime. I updated my question. Would be great if you could put me on the right track. - Niels

1 Answers

1
votes

The answer is quiete simple: sap.ui.base.ManagedObject does not have a method byId. This is a method of sap.ui.core.mvc.View. Just create a View instead of a ManagedObject in beforeEach and you should be fine.

BR Chris