其他分享
首页 > 其他分享> > SAPUI5 Walkthrough Step 28: Unit Test with QUnit

SAPUI5 Walkthrough Step 28: Unit Test with QUnit

作者:互联网

Step 28: Unit Test with QUnit    单元测试   新增文件 webapp/test/unit/model/formatter.js, 对webapp/model/formatter文件中的statusText方法进行测试。
/*global QUnit*/
sap.ui.define([
    "sap/ui/demo/walkthrough/model/formatter",
    "sap/ui/model/resource/ResourceModel"
], function (formatter, ResourceModel) {
    "use strict";
    QUnit.module("Formatting functions", {
        beforeEach: function () {
            this._oResourceModel = new ResourceModel({
                bundleUrl: sap.ui.require.toUrl("sap/ui/demo/walkthrough") + "/i18n/i18n.properties"
            });
        },
        afterEach: function () {
            this._oResourceModel.destroy();
        }
    });
    QUnit.test("Should return the translated texts", function (assert) {
        // Arrange
        // this.stub() does not support chaining and always returns the right data
        // even if a wrong or empty parameter is passed.
        var oModel = this.stub();
        oModel.withArgs("i18n").returns(this._oResourceModel);
        var oViewStub = {
            getModel: oModel
        };
        var oControllerStub = {
            getView: this.stub().returns(oViewStub)
        };

        // System under test
        var fnIsolatedFormatter = formatter.statusText.bind(oControllerStub);

        // Assert
        assert.strictEqual(fnIsolatedFormatter("A"), "New", "The long text for status A is correct");

        assert.strictEqual(fnIsolatedFormatter("B"), "In Progress", "The long text for status B is correct");

        assert.strictEqual(fnIsolatedFormatter("C"), "Done", "The long text for status C is correct");

        assert.strictEqual(fnIsolatedFormatter("Foo"), "Foo", "The long text for status Foo is correct");
        
        assert.strictEqual(fnIsolatedFormatter(""), "xxx", "The long text for status xxx is correct");
    });

});

 

  新增文件 webapp/test/unit/unitTests.qunit.js , 请求上面新建的 webapp/test/unit/model/formatter.js文件
/* global QUnit */
QUnit.config.autostart = false;
sap.ui.getCore().attachInit(function () {
    "use strict";
    sap.ui.require([
        "sap/ui/demo/walkthrough/test/unit/model/formatter"
    ], function () {
        QUnit.start();
    });
});

 

  新增文件 webapp/test/unit/unitTests.qunit.html, 引用上面新建的 webapp/test/unit/unitTests.qunit.js 文件
<!DOCTYPE html>
<html>
<head>
    <title>Unit tests for SAPUI5 Walkthrough</title>
    <meta charset="utf-8">
    <script
        id="sap-ui-bootstrap"
        src="https://sapui5.hana.ondemand.com/1.92.1/resources/sap-ui-core.js"
        data-sap-ui-resourceroots='{
            "sap.ui.demo.walkthrough": "../../"
        }'
        data-sap-ui-async="true">
    </script>
    <link rel="stylesheet" type="text/css" href="https://openui5.hana.ondemand.com/resources/sap/ui/thirdparty/qunit-2.css">
    <script src="https://openui5.hana.ondemand.com/resources/sap/ui/thirdparty/qunit-2.js"></script>
    <script src="https://openui5.hana.ondemand.com/resources/sap/ui/qunit/qunit-junit.js"></script>
    <script src="https://openui5.hana.ondemand.com/resources/sap/ui/qunit/qunit-coverage.js"></script>
    <script src="https://openui5.hana.ondemand.com/resources/sap/ui/thirdparty/sinon.js"></script>
    <script src="https://openui5.hana.ondemand.com/resources/sap/ui/thirdparty/sinon-qunit.js"></script>
    <script src="unitTests.qunit.js"></script>
</head>
<body>
    <div id="qunit"/>
    <div id="qunit-fixture"/>
</body>
</html>

 

执行结果

 

 

 

标签:formatter,QUnit,SAPUI5,28,ui,test,webapp,sap
来源: https://www.cnblogs.com/keyuming/p/15113694.html