繁体   English   中英

在Internet Explorer 9中运行任何测试之前,将调用QUnit安装程序回调

[英]QUnit setup callbacks all being called before running any tests in Internet Explorer 9

在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("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 

如您所见,在未成功运行的情况下,设置回调正确执行了两次,但在执行任何一项测试之前。 由于拆解删除了必需的DOM元素,因此第二个执行的测试注定会失败,因为它的设置被调用为时过早,并且第一个执行的测试(正确)通过拆解将其删除。

当在第一个测试之前执行第二个测试时,总是会出现此问题,但是我看不出有什么理由会引起问题。 该问题从未出现在Chrome中。

我在Chrome中遇到了相同的问题(大约50%的测试失败了),并通过以下方式解决了该问题:

带有错误的代码:

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();
    });
});

QUnit.load()QUnit.start()在测试调用之前就已经移动了,并且可以解决问题。

暂无
暂无

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

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