【问题标题】:How to test a controller with injection如何使用注入测试控制器
【发布时间】:2016-05-11 09:23:15
【问题描述】:

如何用注入测试一个AngularJS控制器,例如:

angular.module('app', []).controller('PasswordController', passwordController);
     passwordController.$inject = ['$scope'];
     //does not exist in the documentation angularjs
   function passwordController($scope) {
      $scope.password = '';
      $scope.grade = function() {
        var size = $scope.password.length;
        if (size > 8) {
          $scope.strength = 'strong';
        } else if (size > 3) {
          $scope.strength = 'medium';
        } else {
          $scope.strength = 'weak';
        }
      };
   }

我无法在测试写作中获得控制器

describe('PasswordController', function() {
  beforeEach(module('app'));
  var $controller;
  beforeEach(inject(function(_$controller_){
       $controller = _$controller_;
  }));

  describe('$scope.grade', function() {
    it('sets the strength to "strong" if the password length is >8 chars', function() {
      var $scope = {};
      var controller = $controller('PasswordController', { $scope: $scope });
      // TypeError: 'undefined' is not a function (evaluating '$controller('PasswordController');
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
  });
});

使用 AngularJS 文档的控制器模型,我设法运行了测试:https://docs.angularjs.org/guide/unit-testing

【问题讨论】:

    标签: angularjs unit-testing controller


    【解决方案1】:

    朝着正确方向迈出的一步是正确实例化您的 $scope 变量:

    var $rootScope, $scope, $controller;
    beforeEach(inject(function(_$rootScope_, _$controller_) {
        $rootScope = _$rootScope_;
        $scope = $rootScope.$new();
        $controller = _$controller_;
    }));
    
    describe('PasswordController', function() {
        var ctrl = $controller('PasswordController',{$scope: $scope});
        // Tests
    });
    

    另一件事 - 肯定在 karma.conf.js 文件中包含控制器的源代码,并且您是否在控制器中调用该函数?你看起来不像……

    编辑

    好的,我已将您的代码合并到我的代码中。这是一个功能齐全的测试套件,用于您编写的内容以及对实际控制器的一些重写:

    控制器:

    (function() {
      'use strict';
    
      angular
        .module('app')
        .component('example', {
          bindings: {},
          controllerAs: "vm",
          controller: "PasswordController",
          templateUrl: 'example.html'
        });
    
      angular
        .module('app')
        .controller('PasswordController', PasswordController);
    
      function PasswordController(){
        var vm = this;
        vm.password = '';
    
        vm.grade = function() {
          var size = vm.password.length;
          if (size > 8) {
            vm.strength = 'strong';
          } else if (size > 3 && < 8) {
            vm.strength = 'medium';
          } else {
            vm.strength = 'weak';
          }
        };
      }
    })();
    

    以及测试(全部通过):

    (function() {
        'use strict';
    
        describe('PasswordController', function() {
    
            var $rootScope, $scope, ctrl;
            beforeEach(module('app'));
    
            beforeEach(inject(function(_$rootScope_, $controller) {
                $rootScope  = _$rootScope_;
                $scope      = $rootScope.$new();
    
                ctrl = $controller('PasswordController',{$scope: $scope});
            }));
    
            describe('PasswordController', function() {
                it('should be defined', function() {
                    expect(ctrl).toBeDefined();
                });
                it('should initalize password to ""', function() {
                    expect(ctrl.password).toBe('');
                });
                describe('grade()', function() {
                    it('should set strength to "strong" if size is greater than 8', function() {
                        ctrl.password = 'password1';
                        ctrl.grade();
    
                        expect(ctrl.strength).toBe('strong');
                    });
                    it('should set strength to "medium" if size is greater than 3', function() {
                        ctrl.password = 'pass';
                        ctrl.grade();
    
                        expect(ctrl.strength).toBe('medium');
                    });
                    it('should set strength to "weak" if size is less than 3', function() {
                        ctrl.password = 'ps';
                        ctrl.grade();
    
                        expect(ctrl.strength).toBe('weak');
                    });
                });
            });
        });
    }());
    

    【讨论】:

    • 谢谢,但在这里测试并没有奏效。还要确保该文件已经是涉及控制器的 karma.conf.js。如果我完全按照文档工作来实现控制器,但必须停止使用 passwordController 注入 $ = [' $ scope 。 "] ;
    • @phdias 没问题 - 接受答案并投票 ;)
    • 我的名誉不允许
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    相关资源
    最近更新 更多