如何在AngularJS中为控制器创建单元测试

时间:2016-10-25 19:40:59

标签: angularjs unit-testing mockups

如何为我的AngularJS控制器LoginController创建单元测试?如何为此示例模拟AuthenticationService? 我的控制器是否正确用于单元测试&使用模型?

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

    }]);

1 个答案:

答案 0 :(得分:0)

尝试https://docs.angularjs.org/guide/unit-testing

Checkout测试控制器部分

引用: -

  

由于控制器在全局范围内不可用,我们需要首先使用angular.mock.inject注入控制器。

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