5

나는 Karma + Jasmine으로 AngularJS 사용자 지정 지시문을 테스트하려고합니다. 웹에서 많은 참조를 확인하는 방법을 찾았습니다. 그러나 해결책은 올바른 방법으로 보이지 않습니다.카르마/재스민 테스트 사용자 지정 지시자 컨트롤러

angular.module("app", []) 
    .directive("test", function() { 
    return { 
     restrict: 'E', 
     scope: { 
     defined: '=' 
     }, 
     templateFile: "test.html", 
     controller: function($scope) { 
     $scope.isDefined = function() { 
      return $scope.defined; 
     }; 
     } 
    }; 
    }); 

describe("Test directive", function() { 
    var elm, scope; 

    beforeEach(module("app")); 
    beforeEach(module("test.html")); 

    beforeEach(inject(function($rootScope, $compile, $injector) { 
    elm = angular.element("<test defined='defined'></test>"); 

    scope = $rootScope; 
    scope.defined = false; 

    $compile(elm)(scope); 
    scope.$digest(); 
    })); 

    it("should not be initially defined", function() { 
    expect(elm.scope().$$childTail.isDefined()).toBe(false); 
    }); 
}); 

이제 지시문 템플릿 파일 인 test.html : 마지막으로

<button data-ng-click='defined = true'></button> 

그리고 karma.conf.js :

module.exports = function(config) { 
    config.set({ 
    basePath: '', 
    frameworks: ['jasmine'], 

    files: [ 
     'angular.min.js', 
     'angular-mocks.js', 
     'test.js', 
     'test.html' 
    ], 

    exclude: [], 

    preprocessors: { 
     "test.html": ['ng-html2js'] 
    }, 

    reporters: ['progress'], 
    port: 9876, 
    colors: true, 
    logLevel: config.LOG_INFO, 
    autoWatch: true, 
    browsers: ['Firefox'], 
    singleRun: true 
    }); 
}; 
의 첫 번째 예를 확인하자, 이것은 test.js입니다

다음 명령 줄을 사용하여 테스트를 실행했습니다.

karma start karma.conf.js 

내게 정말 이상한 것처럼 보이는 것은 컨트롤러에 정의 된 범위 기능은 $$childTail 속성을 통해서만 액세스 할 수 있다는 것입니다. 요소의 범위에서 직접 호출하려고하면 정의되지 않은 값 elm.scope().isDefined()이 있습니다. 누군가가 더 나은 해결책을 가지고 있습니까?

감사합니다.

답변

관련 문제