2016-08-18 3 views
0

지시문이 사용중인 컨트롤러를 테스트해야합니다. 컨트롤러를 참조 할 수 없습니다.AngularJS : 재스민을 사용하는 컨트롤러로 지시문을 테스트하는 동안 컨트롤러가 정의되지 않았습니다.

directive.js

(function() { 
    'use strict'; 

    angular 
     .module('myModule') 
     .directive('cabinet', function() { 
      return { 
       restrict: 'E', 
       replace: true, 
       controller: CabinetThumbnails, 
       controllerAs: 'ctrl', 
       bindToController: true, 
       templateUrl: 'app/components/capture/cabinet.directive.html', 
       scope: { 
        thumbnail: '=' 
       } 
      }; 
     });  

    function CabinetThumbnails($uibModal, cabinetService, SweetAlert, 
           spinner, $state, $window) { 
     var vm = this; 

     vm.showImage = showImage; 
     vm.deleteThumbnail = deleteThumbnail; 

     function showImage() { 
      if (vm.thumbnail.FileCategoryName === 'OpenDocument') { 
       $window.open(vm.thumbnail.FileUrl); 
      } 
      else { 
       $uibModal.open({ 
        animation: true, 
        templateUrl: 'app/components/capture/cabinet.pop-up.html', 
        controller: ModalCtrl, 
        controllerAs: 'ctrl', 
        size: 'lg', 
        resolve: { 
         thumbnailData: function() { 
          return vm.thumbnail; 
         } 
        } 
       }); 
      } 
     } 

     function deleteThumbnail() { 
      //show the popup confirmation 
      SweetAlert.swal({ 
        title: 'Are you sure to delete ?', 
        text: 'You will not be able to recover the file once deleted !', 
        type: 'warning', 
        showCancelButton: true, 
        confirmButtonColor: '#DD6B55', 
        confirmButtonText: 'Delete', 
        closeOnConfirm: true 
       }, 
       function (isConfirm) { 
        if (isConfirm) { 
         spinner.show(); 
         //call the service to delete here 
         cabinetService.deleteCabinetFile(vm.thumbnail.CabinetFileID) 
          .then(function (data) { 
           //validate the response 
           if (data != null && data.returnVal === -1) { 
            SweetAlert.swal('You do not have access to delete ' + 
             'the screen shot', 
             'Please contact the owner of the screen shot!', 
             'error'); 
           } else { 
            //load all the Thumbnails by reloading the page 
            $state.reload(); 
           } 
          }) 
          .catch(function (err) { 
           SweetAlert.swal('Something went wrong !', 
            'Please try again later !', 'error'); 
          }) 
          .finally(spinner.hide); 
        } 
       }); 
     } 

    }  

    function ModalCtrl($scope, $uibModalInstance, thumbnailData, 
         logger, cabinetService) { 
     var ctrl = this; 

     ctrl.thumbnailData = thumbnailData; 
     ctrl.save = save; 
     ctrl.cancel = cancel; 

     //call this method to get executed while the directive loads to open the pop-up 
     getComments(); 

     function getComments() { 
      cabinetService 
       .getComments(thumbnailData) 
       .then(function (data) { 
        ctrl.comments = data; 
       }) 
       .catch(function (err) { 
        logger.error('Unable to get comments, Please try later !', 
         {ttl: 20000}); 
       }); 
     } 
    } 
}()); 

MytestSpec.js :

'use strict'; 

describe('cabinet', function() { 
    var el, 
     CabinetThumbnailsController, 
     scope; 

    beforeEach(bard.appModule('myModule', bard.fakeLogger)); 

    // Initialize the controller and a mock scope 
    beforeEach(function() { 
     bard.inject('$controller', '$compile', '$rootScope', '$q', '$uibModal', 
      'cabinetService', 'SweetAlert', 'spinner', '$state', 
      '$window'); 

     scope = $rootScope.$new(); 

     el = angular.element('<div cabinet></div>', {$scope: scope}); 
     $compile(el)(scope); 
     //CabinetThumbnailsController = el.controller('cabinet'); 
     CabinetThumbnailsController = $controller('CabinetThumbnails', {$scope: scope}); 
     $rootScope.$apply(); 
    }); 

    it('should be initialized', function() { 
     expect(el).toBeDefined(); 
    }); 

    //controller testing 
    it('CabinetThumbNails controller to be initialized', function() { 
     expect(CabinetThumbnailsController).toBeDefined();    
    }); 

    it('CabinetThumbNails controller showImage method to be called', function() { 
     expect(CabinetThumbnailsController.showImage).toHaveBeenCalled(); 
    }); 

}); 

모두 다음 설명도 실패 다음 컨트롤러 메소드를 호출 따라서

 CabinetThumbnailsController = el.controller('cabinet'); 
     CabinetThumbnailsController = $controller('CabinetThumbnails', {$scope: scope}); 

작업과하지 :

expect(CabinetThumbnailsController.showImage).toHaveBeenCalled(); 

답변

1

내 컨트롤러를 다른 파일로 분리하고 컨트롤러를 독립적으로 테스트했습니다. 지시어와 함께 컨트롤러를 테스트하는 접근법을 얻을 수 없었습니다.

관련 문제