AngularJS单元测试控制器和组件

时间:2018-12-12 03:52:03

标签: angularjs unit-testing controller components

我正在使用Karma和Jasmine测试控制器。我的控制器和组件写在两个文件中:

module.component('A', {
  templateUrl: 'template.html',
  controller: 'YourController'
});

在另一个文件中,我定义了控制器:

function() {
    'use strict';

    angular.module('app').controller('YourController',
            YourController);

    YourController.$inject = [ '$scope', '$http', '$window',
            '$rootScope' ];

    function YourController($scope, $http, $window, $rootScope) {       
        function yourcontrollerfunction() {
        }
        yourcontrollerfunction();               
    }
})();

代码正常工作。但是,当我使用Karma和Jasmine测试控制器时,总是会得到一个错误,指出未定义 YourController 。我不明白为什么。请给我任何提示。谢谢!

describe('test1', function () {

  var $componentController;


  beforeEach(module('app'));

  beforeEach(angular.mock.inject(function(_$componentController_) {

  $componentController = _$componentController_;

  }));


  it('test controller', function () {

  var ctrl = $componentController('A',null);

  }); 


}); // end of describe

0 个答案:

没有答案
相关问题