2016-06-06 2 views
0

자스민의 각도 지시 측정 방법에 대한 쓰기 테스트가 필요합니다. toHaveBeen을 테스트하는 방법은 다음 코드에서 .css 메서드를 호출 했습니까? 이 방법으로 스파이를 만들려고했지만 항상 Expected spy css to have been called.이 있습니다. css 메소드 호출을 테스트하는 것이 좋은 생각입니까? 테스트에각도 측정 지시어 - angular.element.css

지침 방법 :

scope.contentPadding =() => { 
    const bars = angular.element(element[0].querySelectorAll('.intro')); 
    if (bars.length > 0) { 
    const bar = angular.element(bars[bars.length - 1]); 
    bar.parent().css('padding-top', `${bar[0].offsetHeight - 10}px`); 
    } 
}; 

내 테스트 구현 :

describe('test', function() { 
    var $rootScope, 
     $compile, 
     $scope, 
     $window, 
     element; 

    function compileDirective() { 
    var html = angular.element(` 
     <div my-directive-name> 
     <div class="section"> 
      <div class="intro"></div> 
     </div> 
     </div>`); 

     element = $compile(html)($scope); 
     $scope.$digest(); 
    } 

    beforeEach(function() { 
     angular.mock.module('my-directive-name'); 

     inject(function(_$rootScope_, _$compile_, _$window_) { 
     $rootScope = _$rootScope_; 
     $compile = _$compile_; 
     $window = _$window_; 

     $scope = $rootScope.$new(); 
     compileDirective(); 
     }); 
    }); 

    describe('padding', function() { 
    it('adds top padding', function() { 
     var el = angular.element(element[0].querySelector('.section')); 
     spyOn(el, 'css'); 
     $scope.contentPadding(); 
     $scope.$digest(); 
     expect(el.css).toHaveBeenCalled(); 
    }); 
    }); 
}); 
+0

이 우리에게 현재 테스트를 표시합니다. –

답변

0

당신이 각 요소를 감시하고 확인 :

it('adds top padding' function() { 
    var element = angular.element(....); 

    spyOn(element, 'css'); 

    // Trigger padding here 

    expect(element.css).toHaveBeenCalled(); 
}); 
+0

답장을 보내 주셔서 감사합니다! 코드를 사용하여 테스트를 구현했지만 '예상 된 스파이 CSS가 호출되었습니다.'라는 응답이 나타납니다. 테스트 구현에 관한 게시물을 완료했습니다. – h3xed

+0

다이제스트주기를 강제 실행하십시오. "scope. $ digest();"를 사용하여 scope.contentPadding(); 뒤에 배치 해보십시오. –

+0

$ scope. $ digest()는 메소드 호출 결과 다음과 같습니다. – h3xed

관련 문제