2010-12-03 13 views
1

나는 매우 가벼운 시스템에서 일하고있는 자바 스크립트 프레임 워크를 테스트하고있다.자바 스크립트 이벤트 처리

필자는 테스트주기를 위반하지 않고 피드백을보고하기 위해 try/catch 내에서 테스트하는 함수를 호출하는 래퍼 역할을하는 테스트 함수를 만들었습니다. 문제는 고의적으로 오류를 만들 때 내 캐치가 호출되지 않는 것입니다. 사전에

내 코드 ....

/// <summary> 
    ///  Acts as a wrapper to allow us to perform and report the result 
    ///  of each individual 
    ///  test without blocking further tests. 
    /// </summary> 
    /// <param name="selector" type="String"> 
    ///  The id jQuery selector e.g #versionTests to repot feedback to. 
    /// </param> 
    /// <param name="testFunction" type="Function"> 
    ///  The test function to call. 
    /// </param> 
    test: function (selector, testFunction) { 

     try { 
      // now we are calling our own callback function 
      if (typeof testFunction === 'function') { 

       testFunction.call(); 
      } 
     } catch (e) { 

     jQuery(selector).addClass("error").append("<p>" + e.description + "</p>"); 

     } 

    } 

감사합니다 ....

편집 ... 추가는 명확성을 위해 코드를했다.

내가 부르고 테스트 기능이 호출

testEqualizeHeight: function() { 

     PeachUI("div.heightTest").equalizeHeight(); 
    } 

....... (this.selector이 jQuerys 선택을 반영하는 속성입니다.) ... 기본적으로 즉,이 호출에 $selector.height(tallest);

equalizeHeight: function() { 
    /// <summary> 
    ///  Equalizes the height of the specified element(s). 
    /// </summary> 

    var $tallest = 0, $selector = this.selector; 

    $selector.each(function() { 

     var $height = jQuery(this).height(); 

     if ($height > $tallest) { 

      $tallest = $height; 
     } 
    }); 

    // ie6 height is the same as min-height for other browsers. 
    if (PeachUI.browser.ie6) { 

     $selector.height(tallest); 

    } else { 

     $selector.css("min-height", $tallest); 
    } 
} 
+0

오류를 생성하는 코드를 게시 할 수 있습니까? – cambraca

답변

2

을 당신이이 놀라운 일 (마지막 라인) 수행하는 것이 나에게 나타나 당신의 source code에서 찾고 : 여기

PeachTest.test("#versionTests", PeachTest.testVersion()); 
PeachTest.test("#browserTests", PeachTest.testBrowser()); 
PeachTest.test("#isNumericTests", PeachTest.testNumeric); 
PeachTest.test("#heightTests", PeachTest.testEqualizeHeight()); 

당신이 PeachTest.testPeachTest.testNumeric에 대한 참조를 전달을하지만, 다른 세 개의 테스트 함수를 호출하고 해당 함수가 PeachTest.test으로 반환하는 값을 전달합니다.

해당 매개 변수 뒤의 함수 호출 연산자 (())를 제거하면 테스트 함수가 예상대로 작동합니다.

+0

나는 내가 어리 석다는 것을 믿을 수 없다! 정말 고맙습니다! :) –

+0

@James - Hehe, 천만에요! –

0

어느 testFunction에 누락 된 '$'가 (이 조건에 싸여)라고 점점되지 않았거나 오류를 추가하는 코드가 잘못 있습니다.

디버거를 사용하여 테스트 실행을 추적하고 생각하는 경로를 따르는 지 확인합니다. 또는 console.logs()

+0

@ hvgotcodes : 오류를 제거하면 실행되는 testfunction이 확실히 호출됩니다. –

2

실제로는 .call()을 호출 할 필요가 없습니다. 당신은 사용할 수 있습니다 : 당신이 실행 컨텍스트를 명시 적으로 설정하려는 경우

testFunction(); 

, 당신은 .call(context, param1, param2, ..)

어떻게 당신이 "의도적으로"오류를 만들려면 어떻게해야합니까 사용할 수 있습니까? exception object이 재산 description하지만 message을 소유하지 않는, 여기에 언급 testFunction

throw new Error('Foo Bar'); 

같은 마지막 것은 내에서 예외를 던져 봅니다.

예 : http://www.jsfiddle.net/RUeEm/

+0

@ jAndy : 설명 속성으로 w3school을 비난 할 수 있습니다. http://www.w3schools.com/js/js_try_catch.asp 나는 그것이 MS 유일한 것임을 깨닫지 못했다. –

+0

@James : W3Schools는 실수, 나쁜 코딩 습관, 끔찍한 예제 등으로 가득 차 있습니다 ** W3Schools는 W3C와 전혀 관련이 없습니다. 보다 나은 안내서는 [MDC] (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Properties_2) (그리고 거기에서'description'은 독점적 속성입니다)을 참조하십시오. –

+0

@JamesSouth : 아무도 탓하지 않습니다. 난 그걸 지적하고 싶었어 :) – jAndy