【发布时间】:2014-03-29 18:26:10
【问题描述】:
我正在尝试在 Qunit 中进行一系列测试。我正在使用 JQM 并且正在使用他们的测试套件,其中包括一个 $.mobile.testHelper 对象,我正在向其中添加方法。
这是我的代码(带有 cmets 和日志):
// my test page is loaded inside an iframe
var frame = document.getElementsByTagName("iframe")[0];
var d = frame.contentDocument;
var w = frame.contentWindow;
var $i = w.$(d);
var $body = w.$("body");
// forcing $(body) as event target
$.testHelper.eventTarget = $body;
// sets "one" listener on "step" event and jumps to next method when triggered
$.testHelper.stepSequence = function (fns) {
$.testHelper.eventSequence("step", fns);
};
// run a test
$.testHelper.runTest = function (command, condition) {
console.log("RUNNING TEST...");
ok(condition, command);
};
// try 10x if a condition is met, wait 1000ms in between
$.testHelper.countDown = function (arr, command) {
var condition, is_done;
var ticker = 0;
var i = w.setInterval(function () {
switch (command) {
case "verifyAttribute":
condition = $i.find(arr[0]).eq(0).attr(arr[1]).indexOf(arr[2]) > -1;
break;
case "waitForElementPresent":
condition = $i.find(arr[0]).length > 0;
break;
}
if (condition) {
console.log("CONDITION PASSED, RUN TEST");
$.testHelper.runTest(command, condition);
w.clearInterval(i);
}
ticker += 1;
console.log(ticker);
if (ticker === 10) {
console.log("FAILED, RUN WITH undefined to fail test");
$.testHelper.runTest(command, condition);
w.clearInterval(i);
}
}, 1000);
};
// my test module
module("UI Basic Interaction");
asyncTest("${base_url}", function () {
expect(2);
// here is my sequence of methods
$.testHelper.stepSequence([
function () {
w.setTimeout(function () {
$body.trigger("step");
}, 800);
$.testHelper.countDown(["div#global-panel", undefined, undefined], "waitForElementPresent");
},
function () {
w.setTimeout(function () {
$body.trigger("step");
}, 800);
$("a:contains('Menu')").trigger("click");
},
function () {
w.setTimeout(function () {
$body.trigger("step");
}, 800);
$.testHelper.countDown(["div#global-panel", "class", "ui-panel-open"], "verifyAttribute");
},
function () {
w.setTimeout(function () {
$body.trigger("step");
}, 800);
$("h1:contains('My Account')").trigger("click");
},
function () {
start();
}
])
});
我需要在测试条件运行后触发“步骤”,但无法让它工作,所以我使用了不合适的setTimeout
我的问题是第一个测试通过,第二个测试正确开始间隔,同时 UI 呈现,但是当找到元素时,QUNIT 几乎在我的控制台报告条件的同时出现Expected 2 assertions, but 1 were run是真的。
问题:
从上面的代码中,我的测试例程中是否有错误,因为 Qunit 出错了,runTest“快”的运行不够?我也很高兴有更好的方法来触发"step"。
谢谢!
【问题讨论】:
-
我对 JQM
testhelper不熟悉,但看起来您的countDown函数最多可以等待 10 秒,但QUnit.start将在大约 4x800 毫秒后运行。 -
没错,现在正在玩弄时机。
-
@psquared:帮帮忙,请看下面我的回答
标签: javascript jquery unit-testing jquery-mobile qunit