[英]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.