2016-10-25 2 views
0

Wie erstelle ich Unit Test für meinen AngularJS Controller LoginController? Wie man AuthenticationService für dieses Beispiel nachbildet? Ist mein Controller ordnungsgemäß für Unit-Tests & mit Mock-Ups gebaut?Wie erstellt man Unit Tests für Controller in AngularJS

-Controller LoginController Code:

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]); 
       } 
      } 
     } 

    }]); 

Antwort

0

Versuchen https://docs.angularjs.org/guide/unit-testing

Kasse einen Abschnitt Controller-Test

Zitiert: -

Da Controller auf dem globalen Bereich nicht verfügbar sind, werden wir müssen Sie angle.mock.inject verwenden, um zuerst unseren Controller zu injizieren.

describe('LoginController', function() { 
     beforeEach(module('app')); 

     var $controller; 

     beforeEach(inject(function(_$controller_){ 
     // The injector unwraps the underscores (_) from around the parameter names when matching 
     $controller = _$controller_; 
     })); 

     describe('My auth test', function() { 
     it('success login will not display validation erreors', function() { 
      var $scope = {}; 
      var controller = $controller('LoginController', { $scope: $scope }); 

      $scope.login(); 
      //test for $scope.validationErrors 
      expect($scope.validationErrors).toBeUndefined(); 
     }); 

     it('failure login will has validation errors', function() { 
      var $scope = {}; 
      var controller = $controller('LoginController', { $scope: $scope }); 

      $scope.login(); 
      //test for $scope.validationErrors 
      expect($scope.validationErrors).not.toBeUndefined(); 
      expect($scope.validationErrors.length).not.toBeGreaterThan(0); 
     }); 
     }); 
    }); 
+0

von meinem Wissen :), es ist nicht Test runing :). Wie wäre es, den Authentifizierungsdienst zu verbessern? Keine Cerdentails gesetzt. Es funktioniert nicht :) –

+0

Dies wird Ihre 'Login()' Funktion testen, die Ihr tatsächlicher Code ist. Sie können seine Auswirkungen auf $ scope oder andere Objekte wie $ location usw. erfassen. Können Sie beschreiben, was genau Sie als eine Testeinheit betrachten? – bhantol

+0

Was ist mit Authentnization Service? es ist Abhängigkeit. Ich dachte mir, dass es nur zum Testen von Controllern gespottet werden sollte. Aber ich kann mich irren. –

Verwandte Themen