2016-10-25 10 views
0

AngularJS 컨트롤러 LoginController에 대한 단위 테스트를 만드는 방법? 이 예제에서 AuthenticationService를 모형화하는 방법은 무엇입니까? 내 컨트롤러가 목업을 사용하여 단위 테스트 &에 맞게 올바르게 빌드되어 있습니까?AngularJS에서 컨트롤러에 대한 단위 테스트를 만드는 방법

컨트롤러 인 LoginController 코드 :

layoutControllers.controller('LoginController', 
    ['$scope', '$rootScope', '$location', 'AuthenticationService', 
    function ($scope, $rootScope, $location, AuthenticationService) { 

     AuthenticationService.ClearCredentials(); 

     $scope.login = function() { 
      $scope.dataLoading = true; 
      AuthenticationService.Login($scope.email, $scope.password, function (response) { 
       AuthenticationService.SetCredentials($scope.email, $scope.password, response.UserId); 
       $scope._showValidationErrors(null, null); 
       $location.path('/'); 

      }, function (response) { 
       $scope._showValidationErrors(response.Errors, response.OtherErrors); 
      }); 
      $scope.dataLoading = false; 
     }; 

     $scope._showValidationErrors = function (errors, otherErrors) { 
      $scope.validationErrors = []; 
      $scope.errors = {}; 
      $scope.errors.form = {}; 

      if (errors && angular.isArray(errors)) { 
       for (var errorCounter in errors) { 
        $scope.validationErrors.push(errors[errorCounter].Message); 
        $scope.errors.form[errors[errorCounter].Key] = errors[errorCounter].Message; 
       } 
       // debugger; 
      } 

      if (otherErrors && angular.isArray(otherErrors)) { 
       for (var errorCounter2 in otherErrors) { 
        $scope.validationErrors.push(otherErrors[errorCounter2]); 
       } 
      } 
     } 

    }]); 

답변

0

가 인용 컨트롤러 섹션

테스트 https://docs.angularjs.org/guide/unit-testing

체크 아웃을 시도해보십시오 -

컨트롤러가 글로벌 범위에서 사용할 수 없습니다 때문에, 우리 컨트롤러를 먼저 주입하려면 angular.mock.inject를 사용해야합니다.

+0

내 지식 :)에서 테스트를 실행하지 않습니다 :). AUthentication 서비스를 조롱하는 것은 어떨까요? 아니 cerdentails 설정합니다. 그것은 작동하지 않습니다 :) –

+0

이것은 실제 코드 인 'login()'함수를 테스트 할 것입니다. $ scope 나 $ location 등의 다른 객체에 영향을 줄 수 있습니다. 테스트중인 단위로 정확히 무엇을 고려하고 있는지 설명 할 수 있습니까? – bhantol

+0

Authetnication 서비스는 어떻습니까? 그것은 의존입니다. 나는 컨트롤러 만 테스트하기 위해 조롱 받아야한다고 생각했다. 그러나 나는 틀릴 수도 있습니다. –