2016-10-25 2 views
0

Comment créer un test unitaire pour mon contrôleur LoginController AngularJS? Comment maquiller AuthenticationService pour cet exemple? Mon contrôleur est-il correctement construit pour les tests unitaires & en utilisant des maquettes?comment créer des tests unitaires pour les contrôleurs dans AngularJS

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

    }]); 

Répondre

0

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

Commander Test d'une section de contrôleur

Citation: -

Parce que les contrôleurs ne sont pas disponibles sur la portée mondiale, nous besoin d'utiliser angular.mock.inject pour injecter notre contrôleur en premier.

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

de ma connaissance :), ce n'est pas runing essai :). Qu'en est-il de la simulation du service AUthentication? Pas de cerdentails. Cela ne fonctionnera pas :) –

+0

Cela va tester votre 'login()' fonction qui est votre code actuel. Vous pouvez capturer ses effets sur $ scope ou d'autres objets tels que $ location etc. Pouvez-vous décrire exactement ce que vous considérez comme une unité en test? – bhantol

+0

Qu'en est-il du service Authetnication? c'est la dépendance. Je pensais que cela devrait être moqué pour tester seulement le contrôleur. Mais j'ai peut-être tort. –