2014-08-27 2 views
0

브로드 캐스트 이벤트 발생시 컨트롤러가 모달을 여는 지 테스트하려고합니다. 코드는 잘 작동하지만 기대는 항상 실패단위 테스트 작성 방법 각도 방송 이후 메서드 호출

it('should open model on idleStart', function() { 
    $scope.$on('idleStart', function(){ 
     expect(dsModalSpy).toHaveBeenCalled(); 
    }); 
    $scope.$broadcast('idleStart'); 

}); 

나는 그것이 때문에 방송의 비동기 특성 실패 가정합니다. 컨트롤러가 모달을 열 시간이 없었습니다. 그래서 다() 메소드에 추가하고 그것은 지금 통과 된 말했다의 setTimeout 기능

it('should open model on idleStart', function (done) { 
    $scope.$on('idleStart', function(){ 
     setTimeout(function(){ 
     expect(dsModalSpy).toHaveBeenCalled(); 
     done(); 
     }, 500); 
    }); 
    $scope.$broadcast('idleStart'); 

}); 

에 넣어,하지만 난 디버거 문에 넣어 그래서 나는 의심을 가지고 있었다. setTimeout의 코드는 테스트가 성공적으로 완료되었다고 표시 될 때까지 실행되지 않습니다.

it('should open modal on idleStart', function (done) { 
    $scope.$on('idleStart', function(){ 
    setTimeout(function(){ 
     debugger //This code is run after the test says it is completed and passed successfully. 
     done(); 
    }, 500); 
    }); 
    $scope.$broadcast('idleStart'); 

}); 

done 인수를 done 함수에 추가하여 done 함수가 호출 될 때까지 완료되지 않았다는 것을 알릴 것이라고 생각했습니다.

감사합니다.

답변

0

npm 설치 karma-jasmine이 나에게 1.x 버전을 제공했습니다.

나는

npm install [email protected]_0 --save-dev 
재스민 2.0을 사용하는 데 필요한