2014-03-29 4 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가 렌더링하는 동안 첫 번째 테스트가 통과하고 두 번째 테스트가 올바르게 시작되지만 그 요소가 발견되면 QUNIT 오류는 내 콘솔과 동시에 거의 Expected 2 assertions, but 1 were run입니다. 조건이 사실이라고보고합니다.

질문 : 위의 코드에서
충분히 runTest "빨리"실행되지 않습니다 내 테스트 과정에서 오류가 있기 때문에 Qunit 오류 아웃이? 또한 "step"을 트리거하는 더 좋은 방법에 만족합니다.

감사합니다.

+1

JQM'testhelper'에 익숙하지 않지만'countDown' 함수가 10 초까지 기다릴 수는 있지만 QUnit.start는 약 4x800ms 후에 실행됩니다. – psquared

+0

사실, 지금 타이밍을 가지고 놀아 라. – frequent

+0

@psquared : helped, 아래 나의 답변 참조 – frequent

답변

0

확인. 잘못은 무엇

: 많은 참견 후

  • 내 클릭 선택이 고정 iframe이 $i.find("eleme... 대 문서를 검색 $(element:contains(...)했다.
  • 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"); 
     } 
    ]) 
}); 

비올라. 멋지게 작동합니다.

관련 문제