2013-04-26 3 views
0

사용자가지도에 표시되는 위치를 검색 할 수있는 자바 스크립트 애플리케이션이 있습니다. 위치 검색은 서버 애플리케이션에 대한 AJAX 호출로 수행됩니다. 이 서버 통신 (searchComponent)에 대한 책임이있는 구성 요소가 하나 있으며 통신이 작동하는 경우 단위 테스트를 원합니다. 가장 중요한 방법은사용자 정의 AJAX 이벤트를 단위 테스트하는 방법

  • EVENT.publish(data)
    있습니다

    var topics = {}; 
    jQuery.Topic = function(id) { 
        var callbacks, 
        method, 
        topic = id && topics[ id ]; 
        if (!topic) { 
         callbacks = jQuery.Callbacks(); 
         topic = { 
          publish: callbacks.fire, 
          subscribe: callbacks.add, 
          unsubscribe: callbacks.remove 
         }; 
         if (id) { 
          topics[ id ] = topic; 
         } 
        } 
        return topic; 
    }; 
    

    : 사용자 정의 이벤트

    내 사용자 정의 이벤트의

    설명 http://api.jquery.com/jQuery.Callbacks/에서 가져온 간단한 게시/구독 구현입니다 Trigg 이벤트를 보내고 모든 가입자에게 데이터를 전달합니다.

  • EVENT.subscribe(callback) 이벤트가 콜백 함수가 실행될 때마다 EVENT에 콜백 함수를 가입 시키십시오.

사용 사례

기본 워크 플로는 이것이다 :

  • 사용자가 검색 버튼을 클릭
  • 응용 프로그램이 searchComponent.doSearch()
  • searchComponent.doSearch() 호출을 서버로 요청을 전송
  • 서버 r esponds
  • searchComponent은 트리거하는 사용자 정의 중 하나SEARCH_RESULT 또는 SEARCH_FAILED
  • 응용 프로그램 (오류 메시지가지도에 표시 일 또는 생산) 두 이벤트를 청취하고 거기에서 계속

SEARCH_RESULT에 대한 이벤트 및 SEARCH_FAILED은 위에서 설명한 사용자 지정 이벤트입니다.

var SEARCH_RESULT = jQuery.Topic('SEARCH_RESULT'); 
var SEARCH_FAILED = jQuery.Topic('SEARCH_FAILED'); 

무엇을하고 싶습니까? searchComponent가 작동하는지

나는 테스트 할 :

  • 가 제대로 만들어지고 서버에 대한 요청이 있습니까?
  • 서버의 응답이 올바르게 처리 되었습니까?
  • 유효하지 않은 전화를 걸면 검색에 실패합니까?
  • 서버가 다운 된 경우 시간 초과는 어떻게됩니까?

평소의 것들.;)

질문 (마지막으로)) I이 사용 사례를 테스트 할 수있는 방법

? (나는 다른 자바 스크립트 테스트 프레임 워크에있는 모든 제안을 개방적이야 불구하고 양호하게는 JS-테스트 드라이버를 사용)

답변

0

[AsyncTestCase] ​​[1]에서 JS 테스트를 사용하여 비동기 동작을 테스트 할 수 있습니다 -운전사.

다음은 doSearch() 메소드의 테스트 케이스 예제입니다. 테스트의 각 부분에서 무엇을하는지 설명하기 위해 주석을 추가했습니다.

MyTest = AsyncTestCase("MyTest", { 

testSearchGoodCase : function(queue) { 
    // this flag will only be set to true when SEARCH_RESULT 
    // is triggered (see STEP 1) 
    var resultReceived = false; 

    // STEP 1 
    queue.call('prepare callbacks', function(callbacks) { 

     // add event handler for the desired behaviour 
     var onExpectedBehaviour = callbacks.add(function() { 
      // when this method is called the queue will enter STEP 2 
      resultReceived = true; 
     }); 

     SEARCH_RESULT.subscribe(onExpectedBehaviour); 

     // What if our test-method is not working and an error occurs? 
     // Then the SEARCH_FAILED event is triggered, but for this 
     // test case that is an error. So we can add the onError method 
     // as an errback in js-test-driver. 

     // errbacks cause the test to fail with a custom message. 
     var onError = callbacks.addErrback('Received an error.'); 

     // When SEARCH_FAILED is triggered onError is called 
     // -> that will cause the test to fail 
     // with the error message "Received an error". 
     SEARCH_FAILED.subscribe(onError); 

     // Now everything is set up and we can execute the function 
     // that we want to test. 
     searchComponent.doSearch(); 

     // The test will then run until either SEARCH_RESULT 
     // or SEARCH_FAILED is triggered. Or the test will 
     // timeout (if none of the two events is triggered). 
    }); 

    // STEP 2 
    queue.call('check conditions after asynchronous method completed', function() { 

     // when we get here the doSearch() method completed 
     // its asynchronous task. 
     assertTrue(resultReceived); 

    });   
} 
} 
관련 문제