簡體   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