2016-06-24 4 views
3

Jasmine이있는 구성 요소에 대해 1.5.6 단위 테스트를 수행하려고합니다. 그러나 나는 아래의 오류를 계속 받고 이유를 모르고있다.

오류 : 미정은 ('controller.name'를 평가) 목적 아니다

성분 :

function MainViewerCtrl() { 
    this.$onInit = function(){ 
     console.log('Component main viewer initialized!'); 
    }; 
    this.name = 'Main Component!'; 
} 

angular.module('ks').component('mainViewer', { 
    bindings: { 
     name : '@' 
    }, 
    controller : MainViewerCtrl, 
    templateUrl: "mainViewer.html" 
}); 

규격 :

describe('Component : mainViewer', function(){ 

    beforeEach(angular.mock.module('ks')); 

    describe('with $componentController', function() { 

    var controller, scope; 

    beforeEach(inject(function($rootScope, $componentController){ 

     scope = $rootScope.$new(); 
     controller = $componentController('mainViewer', 
             {$scope:scope}, 
             {name: 'Main Component!'}); 
    })); 

    it('should have my binding bound', function() { 

     expect(controller.name).toBeDefined(); 
     expect(controller.name).toBe('Main Component!'); 
    }); 
}); 

});

내가 뭘 잘못하고 있니?

답변

-1

대신 _$componentController_을 주입해야합니다. 페이지 https://docs.angularjs.org/guide/component

var $componentController; 
beforeEach(inject(function($rootScope, _$componentController_){ 
    scope = $rootScope.$new(); 
    $componentController = _$componentController_; 
    controller = $componentController('mainViewer', 
            {$scope:scope}, 
            {name: 'Main Component!'}); 
})); 
+2

당신은 이것을 할 필요가 없습니다 ...'_ $ componentController_'의 밑줄은 당신이 그것을 주입 할 수 있고'$ componentController'라는 변수 이름을 사용하지 못하게하는 관례입니다. $ componentController가 나 자신을 정의하지 못하게되었습니다. – gonzofish

1

시도에서 확인 마지막 테스트 예는 PhantomJS없이 카르마를 실행하는 것이 아니라 크롬이나 파이어 폭스에 있습니다. 나는 PhantomJS와 함께 angular.mock.inject을 사용하여 inject 함수에서 궁극적으로 (그리고 내 단위 테스트 논리가 필요하지 않은 경우에도) 정의되지 않은 매개 변수를 이끌어 내 테스트를 실패하는 것으로 나타났습니다. 구성된 브라우저에서 PhantomJS를 제거하면 근본 원인을 모르고 있었지만 적어도 테스트를 계속할 수있었습니다.

2

시도해 보셨습니까/this answer을 보았습니까?

나는 결국 그것이 내가 카르마 설정에서 파일에 추가 할 수 없습니다 내 응용 프로그램 (UI 라우터, ngResource)에 모듈 의존성을 가지고 밝혀졌다 $ componentController이 정의되었습니다 비슷한 문제가 있었다. 그들이 적재되지 않았기 때문에 주사가 제대로 작동하지 않았습니다.

+0

누락 된 종속성을 어떻게 알 수 있습니까? 나는 같은 문제가있다 : http://stackoverflow.com/questions/41984613/how-to-test-angular-1-6-component-with-injected-service – DonJuwe

관련 문제