繁体   English   中英

在单元测试中调用外部函数

[英]Call external function in unit test

我正在尝试学习qunit测试; 我是OpenUi5开发人员,每天都使用Webstorm IDE。 Webstorm允许我配置Karma运行配置。 我创建了一个简单的业力配置文件,并test1.js了一个测试文件test1.js

test("Test function sum()",
  function() {
    ok(sum(0, 0) == 0, "the sum of 0 with 0 is 0");
    ok(sum(2, 0) == 2, "the sum of 2 with 0 is 2");
    ok(sum(2, 1) == 3, "the sum of 2 with 1 is 3");
    ok(sum(2, -1) == 1, "the sum of 2 with -1 is 1");
    ok(sum(-2, 1) == -1, "the sum of -2 with 1 is -1");
  });


function sum(a, b) {
  return a + b;
};

好! sum函数位于同一文件中。 但是现在我要开始在项目的js文件夹中测试功能(单元测试在test-resources文件夹中); 例如在js/util.js我有getShortIdGrid函数:

//util.js file
jQuery.sap.declare("ui5bp.control");

ui5bp.control = {
  ...
  getShortIdGrid: function(sFromId) {
      if (sFromId.lastIndexOf("_") < 0)
        return sFromId;
      else
        return sFromId.substring(0, sFromId.lastIndexOf("_"));
  },
  ...
}

这是我的测试:

test("test getShortIdGrid",
    function () {
        equal( ui5bp.control.getShortIdGrid("shortId_123"), "shortId" ,"ShortIdGrid of 'shortId_123' is 'shortId'" );
    });

如何在测试中调用ui5bp.controlgetShortIdGrid

Karma控制台向我显示ReferenceError:尚未定义ui5bp

正确加载util.js文件后,如何管理声明jQuery.sap.declare("ui5bp.control"); 在顶端? 我想测试我的简单功能,而不必泄漏整个库!

您需要确保unit.js包含在业力配置文件files属性中,以使其对业力可用。 例如,如果您的unit.js位于/ src文件夹中,而规格位于/ tests中,则其外观如下:

module.exports = function (config) {
    config.set({

        // base path, that will be used to resolve files and exclude
        basePath: '',
        frameworks: ['qunit'],
        files: [
            'tests/**/*.js',
            'src/*.js'
        ],
        exclude: [],
...

足以解决ui5bp

暂无
暂无

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

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