2014-03-29 50 views
1

我試圖在Qunit中獲得一系列測試工作。我正在與JQM合作,並使用他們的測試套件,其中包括一個$.mobile.testHelper對象,我正在爲其添加方法。爲什麼Qunit不能捕捉我正在運行的2+測試?

這裏是我的代碼(意見和日誌):

// 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呈現,但是當元素被發現時,幾乎同時出現了Expected 2 assertions, but 1 were run錯誤我的控制檯報告情況爲真。

問:
從上面的代碼,有沒有在我的測試程序的錯誤,不運行runTest「快」不夠,因爲Qunit出錯了呢?此外,我會很高興爲更好的方式來觸發"step"

謝謝!

+1

我對JQM'testhelper'不熟悉,但它看起來像'countDown'函數可能會等待10秒,但'QUnit.start'將在大約4x800ms後運行。 – psquared

+0

真的,現在玩的時機。 – frequent

+0

@psquared:有幫助,請參閱下面的答案 – frequent

回答

0

好的。

什麼是錯的:多管閒事後

  • 我點擊選擇爲$(element:contains(...)其搜索的文件VS修復了這個iframe中$i.find("eleme...
  • 我添加了第二位聽衆test_runner,一旦測試運行就會觸發。只有在所有測試運行後,我纔會觸發start()。這樣Qunit必須等待:-)

和工作代碼(見註釋(1),(2),(3)更改):

var frame = document.getElementsByTagName("iframe")[0]; 
var d = frame.contentDocument; 
var w = frame.contentWindow; 
var $i = w.$(d); 

// (1) set counter for tests ran 
// This allows to trigger start() after all tests are done 
var test_log = 0; 
var $body = w.$("body"); 

$.testHelper.eventTarget = $body; 
$.testHelper.stepSequence = function (fns) { 
    $.testHelper.eventSequence("step", fns); 
}; 
$.testHelper.runTest = function (command, condition) { 
    ok(condition, command); 
    $body.trigger("step"); 
    // (2) When running a test, also trigger a runner on body to log no of tests 
    $body.trigger("test_runner"); 
}; 
$.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("PASSED TEST"); 
      $.testHelper.runTest(command, condition); 
      w.clearInterval(i); 
     } 
     ticker += 1; 
     if (ticker === 10) { 
      console.log("FAILED"); 
      $.testHelper.runTest(command, condition); 
      w.clearInterval(i); 
     } 
    }, 1000); 
}; 

module("UI Basic Interaction"); 
asyncTest("${base_url}", function() { 
    expect(2); 
    $.testHelper.stepSequence([ 
     function() { 
     // (3) set a listener for tests ran 
     // once all tests are done, start() 
      $body.on("test_runner", function (e) { 
       test_log += 1; 
       if (test_log === 2) { 
        start(); 
       } 
      }); 
      $body.trigger("step"); 
     }, 
     function() { 
      $.testHelper.countDown(
       ["div#global-panel", undefined, undefined], 
       "waitForElementPresent" 
      ); 
     }, 
     function() { 
      $i.find("a:contains('Menu')").trigger("click"); 
      $.testHelper.countDown(
       ["div#global-panel", "class", "ui-panel-open"], 
       "verifyAttribute" 
      ); 
     }, 
     function() { 
      $i.find("h1:contains('My Account')").trigger("click"); 
     } 
    ]) 
}); 

中提琴。很好地工作。

相關問題