【发布时间】:2015-08-28 10:13:26
【问题描述】:
我正在尝试学习 qunit 测试;我是一名 OpenUi5 开发人员,我每天都使用 Webstorm IDE。
Webstorm 允许我配置 Karma 运行配置。我创建了一个简单的业力配置文件,并编写了一个测试文件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 is not defined。
正确加载 util.js 文件后,如何管理顶部的声明 jQuery.sap.declare("ui5bp.control");?我想在不重新加载完整库的情况下测试我的简单函数!
【问题讨论】:
-
你是如何在测试中包含 util.js 文件的?它会导出
ui5bp对象吗? -
这正是问题所在:我不知道如何包含
unit.js。使用 Openui5 框架,要使用 controlgetShortIdGrid 函数,我通常根据需要定义 `jQuery.sap.require("js.util");` 在我的 Component 文件和 util.js 文件顶部的文件我写jQuery.sap.declare("ui5bp.control");然后我可以通过ui5bp.control.getShortIdGrid调用函数
标签: javascript unit-testing karma-runner webstorm sapui5