0

如何为我的 AngularJS 控制器 LoginController 创建单元测试?如何为这个例子模拟 AuthenticationService?我的控制器是否为单元测试和使用模型正确构建?

控制器登录控制器代码:

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

    }]);
4

1 回答 1

0

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

Checkout Testing a Controller section

Quoting:-

Because controllers are not available on the global scope, we need to use angular.mock.inject to inject our controller first.

    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);
        });
      });
    });
于 2016-10-25T19:46:47.670 回答