2
카르마는 TypeError: Cannot read property 'originalPath' of undefined
이 (가) link
기능에 다음과 지시어를 사용해 던지는 유지 :유닛 테스트 방법 지시 링크 기능에서 각도 routeChangeSuccess?
angular.module('myApp').directive('sidebar', ['$route', function ($route)
{
return {
restrict: 'E',
templateUrl: 'views/sidebar.html',
scope: {
activeNav: '@'
},
link: function (scope, element, attrs) {
scope.$on('$routeChangeSuccess', function (event, curr, prev) {
scope.activeNav = curr.$$route.originalPath || '/about';
});
}
}
및 단위 테스트 :
describe('sidebar directive', function() {
var $compile,
$rootScope,
scope,
element;
beforeEach(module('MyApp'));
beforeEach(module('my.templates')); // ng-html2js karma template loader
beforeEach(inject(function(_$compile_, _$rootScope_){
$compile = _$compile_;
$rootScope = _$rootScope_;
scope = $rootScope.$new();
}));
it('defaults to /about route', function() {
element = $compile("<sidebar></sidebar>")(scope);
var linkScope = element.children().scope();
$rootScope.$broadcast('$routeChangeSuccess');
$rootScope.$digest();
expect(linkScope.activeNav).toBe('/about');
});
});
링크 내에서 curr
경로를 기록
Object{params: Object{}, pathParams: Object{}, locals: Object{}}
를 얻을. 나는 모의 라우트를 브로드 캐스트 메시지에 전달하려고 시도했지만 아무 것도 변경되지 않았습니다. 지시문에 예상되는 (기본) 경로를 전달하려면 어떻게해야합니까? 링크 내에서 경로 변경을 추적하는 것이 올바른 방법일까요? 제 생각 엔 Jasmine과 유닛 테스팅을 처음 접했을 것입니다.