Angularjs 使用Karma对角Js控制器进行单元测试

Angularjs 使用Karma对角Js控制器进行单元测试,angularjs,unit-testing,karma-jasmine,Angularjs,Unit Testing,Karma Jasmine,我有一个指令,其中控制器被定义为指令定义的一部分 (function(){ var app = angular.module('app', []); app.directive('test', function(){ return { restrict: 'E', templateUrl: 'test.html', controller: ['$scope', function ($scope){...*set of function

我有一个指令,其中控制器被定义为指令定义的一部分

 (function(){
     var app = angular.module('app', []); 
    app.directive('test', function(){ 
    return { 
    restrict: 'E', 
    templateUrl: 'test.html', 
    controller: ['$scope', function ($scope){...*set of functions*......}], 
    controllerAs:'reportCtrl',
    link:function(Attrs){
       }
     }; 
   }); 
})(); 

我需要为函数集编写单元测试。我对业力测试还不熟悉,我没有找到太多关于这类代码的资源。我几乎尝试了所有可能的方法。有人能帮我写一个单元测试吗这里是一个例子。见演示

我所做的是编译指令并称之为摘要循环。 安装后,它与常规控制器基本相同

describe('Auth Service Tests', function() {
  var element;
  var isolated;
  var $scope;
  beforeEach(module('MyApp'));
  beforeEach(inject(function($rootScope, $compile, $templateCache) {
    // Imports test.html
    var templateUrl = 'test.html';
    var req = new XMLHttpRequest();
    req.onload = function() {
      $templateCache.put(templateUrl, this.responseText);
    };
    req.open('get', templateUrl, false);
    req.send();


    $scope = $rootScope.$new();

    element = '<test></test>';
    // Creates the directive.
    element = $compile(element)($scope);

    // activates the $digest cycle.
    $scope.$apply();
  }));

  it('tests the transclude', function() {
    expect($scope.apple).toEqual('world');
    expect($scope.getApple()).toEqual('world');
  });

});

非常感谢。我有一个模板Url:../test/test/test.html,我正在另一个文件夹中编写单元测试。我应该给出$templateCache.put中单元测试文件夹中test.html的路径吗。我收到一个错误请求:GET../test/test/test.htmlyes,您应该更改示例以适合您的路径。或者,您可以使用NGHTML2JS预处理器。这将把html转换成js,并将其放在单个模块中。这样您就不必使用$templateCache。如果我们使用ngHtml2JsPreprocessor,那么我们只需要加载模块,就可以避免模板缓存,对吗?我不知道我犯了什么错误。。在karma.conf.js中,我有到html的链接,并为nghtml2jspreprocessor下的模块命名。我收到一个错误,说模块不可用。可能是您的karma.conf.js设置不正确。可能会为此问题创建一个新帖子。可能重复