Javascript 在Internet Explorer 9中运行任何测试之前,QUnit安装程序将回调所有被调用的测试
在IE9中执行QUnit测试时,我遇到了一个非常奇怪的问题。测试结构如下所示:Javascript 在Internet Explorer 9中运行任何测试之前,QUnit安装程序将回调所有被调用的测试,javascript,internet-explorer-9,qunit,Javascript,Internet Explorer 9,Qunit,在IE9中执行QUnit测试时,我遇到了一个非常奇怪的问题。测试结构如下所示: function setupTest() { // Adding some extra items to the DOM } function teardownTest() { // Removing dirtied items from the DOM } module("MODULE A", {setup: setupTest, teardown: teardownTest}); test(
function setupTest() {
// Adding some extra items to the DOM
}
function teardownTest() {
// Removing dirtied items from the DOM
}
module("MODULE A", {setup: setupTest, teardown: teardownTest});
test("TEST 1", function () {
// Some simple assertions
});
module("MODULE B", {setup: setupTest, teardown: teardownTest});
test("TEST 2", function () {
// Some simple assertions
});
大约50%的时间这些测试失败。我已确保测试完全独立,并且在测试运行之前/之后,安装/拆卸完全完成。问题归结为设置回调有一半的时间是按顺序调用的。通过日志记录,我可以看到这种情况:
成功运行:
LOG: completing setup
LOG: MODULE A: TEST 1
LOG: completing teardown
LOG: completing setup
LOG: MODULE B: TEST 2
LOG: completing teardown
未成功运行:
LOG: completing setup
LOG: completing setup <-- ?
LOG: MODULE B: TEST 2
LOG: completing teardown
LOG: MODULE A: TEST 1
LOG: completing teardown
正如您在未成功运行时看到的,安装回调被正确执行两次,但在执行任何一个测试之前。由于teardown删除了所需的DOM元素,第二个执行的测试肯定会失败,因为它的设置调用得太早,而第一个执行的测试用它的teardown正确地将其删除
当在第一个测试之前执行第二个测试时,总是会出现此问题,但我看不出这会导致问题的原因。这个问题从来没有出现在Chrome中。我在Chrome中有50%的测试失败,我用下一种方法解决了这个问题: 带有bug的代码: 修正版
QUnit.load和QUnit.start在调用测试之前被移动,它完成了这项任务。测试并不完全独立。如果他们是,你就不会有这个问题。即使他们不是独立的,我向你保证他们是独立的,因为我已经把他们缩减到除了测试DOM结构之外什么都不做,就像他们在安装后所期望的那样,那也不能解释为什么在任何测试运行之前,这两个测试都会调用安装回调,而不是像应该发生的那样在每个测试运行之前。它可能与这个问题有关,也可能与这个问题无关:这个对测试的拉取请求可能是一个QUnit Bug:
require(["jquery", "jquerymobile", "QUnit", "underscore",
"units/general.unit", "units/mapping.unit"],
function( $, jqm, __QUnit, _ ) {
var test_modules = Array.prototype.splice.call(arguments, 4);
_.each( test_modules, function(test){
test.start();
});
QUnit.load();
QUnit.start();
});
require(["jquery", "jquerymobile", "QUnit", "underscore",
"units/general.unit", "units/mapping.unit"],
function( $, jqm, __QUnit, _ ) {
QUnit.load();
QUnit.start();
var test_modules = Array.prototype.splice.call(arguments, 4);
_.each( test_modules, function(test){
test.start();
});
});