2014-03-29 46 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"); 
     } 
    ]) 
}); 

中提琴。很好地工作。

相关问题