2013-02-15 5 views
0

내 지시선 컨트롤러에 함수가 있는데 테스트를 시도하고 어떤 리소스도 찾을 수 없습니다. 나는이 오류 형식 오류를 던지고 각지시어 컨트롤러 기능을 테스트하는 방법은 무엇입니까?

    before each { 
        scope = $rootScope ; 
        $compile(element)(scope); 
        scope.$digest(); 
        } 


      it('should update days when datepicker is changed', function() { 
       scope.seldate = new Date('4/11/2014'); 
       scope.stdate = new Date('4/1/2014'); 
       scop`enter code here`e.days = 10; 
       scope.$digest(); 
       scope.$apply(function() { 
        scope.seldate = new Date('4/12/2014'); 
        scope.datePickerChange(); // This is a function in my directive controller 
       }); 
       expect(scope.days).toBe(11); 

      }); 

app.directive('mydirective',function(){ 
     return { 
       restrict:'E', 
       scope:{ 
       days: '=', 
       selectedDate: '=', 
       startDate: '=' 

       }, 
       $scope.datePickerChange = function() { 
        //Sod is helper for start of the day with hours/mins/seconds set to 0 
       $scope.days = moment(new Date($scope.selectedDate)).sod().diff($scope.getStartDate(), 'days'); 
       }; 
      }; 


      }); 

전에이 : 그 너머

app.directive('mydirective',function(){ 
     return { 
       restrict:'E', 
       scope:{ 
       days: '=', 
       selectedDate: '=', 
       startDate: '=' 
       }, 
       link: function(scope, elem, attrs) { 
       scope.datePickerChange = function() { 
        //Sod is helper for start of the day with hours/mins/seconds set to 0 
        scope.days = moment(new Date($scope.selectedDate)) 
           .sod().diff($scope.getStartDate(), 'days'); 
       }; 
       } 
     } 
}); 

당신의 지시를 테스트하기 : 개체 번호가있는 방법 'datePickerChange'

답변

1

귀하의 지시 선언이 무효가 없습니다 Angular 's github repo에서 볼 수있는 예를 따라야합니다. ngSwitch is a good example, IMO

기본 개념은 $ compile을 사용하여 지시문을 실행하고 검사하는 것입니다.

it('should do something', inject(function($rootScope, $compile) { 
    var scope = $rootScope.$new(); 
    var element = $compile('<my-directive></my-directive>')(scope); 

    //assert things. 
    expect(scope.days).toEqual(somethingHere); 
    scope.datePickerChange(); 
    expect(scope.days).toEqual(somethingElse); 
}); 
관련 문제