2014-06-06 3 views
0

일반적으로 Jasmine 및 단위 테스트를 통한 단위 테스트는 완벽한 루키입니다. 최근 작업으로 AngularJS 컨트롤러를 단위 테스트하려고합니다. 아래에서 당신은 내 코드와 내가 시도한 것을 볼 수 있으며, 대부분은 내가 갈 곳을 찾지 못했다. 어떤 도움을 주시면 감사하겠습니다 !!재스민을 통한 단위 테스트 AngularJS 컨트롤러

컨트롤러

angular 
     .module('TDE') 
     .controller('LocationController', ['$rootScope', '$scope', '$location', '$window', '$document', 'LocationService', 'HeaderFooterService', 'SearchService', 'TranslationService', 'MTDE_CONFIG', 'LocationPartnerAssignmentService', 'ExperimentService', function ($rootScope, $scope, $location, $window, $document, $LocationService, $HeaderFooterService, $SearchService, $TranslationService, $MTDE_CONFIG, $LocationPartnerAssignmentService, $ExperimentService) { 

     //some non important code 
     //What I'm trying to test 
     $scope.boolTest= function(var){ 
      return true; //this method only returns true 
     } 
     //all I'm after is basic understanding, I've done countless searches but I can't get it 

    //other code 

도우미 파일 (테스트 파일)

beforeEach(function() { 
     angular.mock.module('LocationController'); // 
     inject(function ($injector) { 
      //$rootScope = $injector.get('$rootScope'); 
      //$scope = $rootScope.$new(); 
      //ctrl = $injector.get('$controller')("LocationController", { $scope: $scope }); 
      //Neither ctrl really functions that well 

      injector = $injector; 
      ctrl = $injector.get('$controller'); 
      scope = $injector.get('$rootScope').$new(); 
     }); 
    }); 

Spec.html 지금 나는 끼 었어 현재로

<!-- jasmine source --> 
    <link rel="shortcut icon" type="image/png" href="spec/lib/jasmine-1.2.0/jasmine_favicon.png"> 
    <link rel="stylesheet" type="text/css" href="spec/lib/jasmine-1.2.0/jasmine.css"> 
    <script type="text/javascript" src="spec/lib/jasmine-1.2.0/jasmine.js"></script> 
    <script type="text/javascript" src="spec/lib/jasmine-1.2.0/jasmine-html.js"></script> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script> 
    <script type="text/javascript" src="https://code.angularjs.org/1.1.0/angular-mocks.js"></script> 

    <!-- include source files here... --> 
    <script type="text/javascript" src="js/Location/LocationController.js"></script> 
    <!-- include spec files here... --> 
    <script type="text/javascript" src="spec/RTHelper.js"></script> 

, 나는 정말 기본적인 테스트를 얻을 수 있습니다 a + b와 같이 작동하고 결과를 확인하십시오. 그러나 필자는 $ scope에 대한 액세스를 얻을 수는 없습니다. ethod.

현재 오류 없음 모듈입니다 : 나는 'TDE'를 사용하지만, 아무것도, 누군가가 나를 도와주세요에 의해 그것을 시도했습니다 LocationController

!

답변

1

이전 링크 죄송합니다. 귀하의 문제를 이미 이해하지 못했습니다.

단위 테스트를 설정하는 경우 Angular code를 테스트하고 CI 환경에 통합하는 가장 쉬운 방법 인 Karma (http://karma-runner.github.io/0.12/index.html)를 확인하는 것이 좋습니다.

어쨌든, 여기 재스민의 기본 컨트롤러를 테스트의 예

function MyControllerCtrl ($scope, Service) { 
    $scope.something = true; 
    $scope.set = Service.set; 
}; 

describe('MyControllerCtrl', function() { 

    // load the controller's module using angular.mock.module 
    beforeEach(module('myApp')); 

    var subject 
    , $scope 
    , Service = { // mock the Service service 
     set: function() {}, 
     } 
    ; 

    beforeEach(inject(function ($controller, $rootScope) { 
    $scope = $rootScope.$new(); // create a new clean scope 
    subject = $controller('MyControllerCtrl', { 
     Service: Service, 
     $scope: $scope, 
    }); 

    })); 

    it('should do something', function() { 
    expect($scope.something).toEqual(true); 
    }); 

    it('bind Service.set', function() { 
    expect($scope.set).toEqual(Service.set); 
    }); 

}); 

이 뒤에 아이디어는 각 시험 전에, 당신이 컨트롤러를 통과한다는 것입니다 귀하의 조롱 서비스 ($ 범위는 서비스에 불과하다) , 컨트롤러가해야 할 일을하는지 테스트합니다.

귀하의 경우 ($ scope.boolTest())를 기대할 수 있습니다. toEqual (true);

+0

어떤 이유로 컨트롤러에서 내 코드를 확인하고 싶지는 않지만 문제가 있습니다. beforeEach (모듈 ('stuff')) 조합을 모두 시도했습니다. 그냥 내가 갈 수 있도록하지 않는 것 같습니다. 나는 내가 거기에서 일하는 것을 알고 다른 파일을 넣었으므로 경로 문제가 아닐 것이라고 확신한다. $ controller ('LocationController', {$ scope : $ rootScope})를 시도 할 때마다 실패하고 "Error : Argument 'LocationController'는 함수가 아니며 정의되지 않았습니다. 다른 아이디어가 있습니까? – user3716084

+0

그냥 파일이로드되었는지 확인했습니다.이 오류는 다음과 같습니다. 잡히지 않은 오류 : 모듈 없음 : TDE – user3716084

+0

모듈 선언을 별도의 파일로 사용하는 경우 서비스 및 컨트롤러 정의 앞에 다음과 같이로드하십시오. file1. js angular.module ('TDE', []); file2.js angular.module ('TDE'). 컨트롤러 ('LocationController', function ...) 그런 다음 테스트에서 종속성을 주입하기 전에 TDE 모듈을 모방해야합니다 : beforeEach (module ('TDE')); beforeEach (inject (function ($ controller) { 제목 = $ 컨트롤러 ('LocationController', {/ * 종속성 * /}) }))); – DrDyne

관련 문제