Unit testing AngularJS中的单元测试指令控制器

Unit testing AngularJS中的单元测试指令控制器,unit-testing,angularjs,controller,jasmine,directive,Unit Testing,Angularjs,Controller,Jasmine,Directive,我试图完全理解如何用Jasmine测试AngularJS中的指令。目前,我有一个指令,如下所示: .directive('myDirective', function() { return { restrict: 'E', transclude: true, replace: true, scope: { title:'=', state:'@' },

我试图完全理解如何用Jasmine测试AngularJS中的指令。目前,我有一个指令,如下所示:

.directive('myDirective', function() {
    return {
        restrict: 'E',
        transclude: true,
        replace: true,
        scope: {
          title:'=',
          state:'@'
        },
        templateUrl: 'myHtml.tpl.html',
        controller: function ($scope) {
          // Business Logic Specific to the Directive goes in here
        }
    };
})
正如上面的代码片段所示,我的指令在指令定义中有一个控制器。我试图测试的具体指令相当复杂。指令中的控制器有200多行代码。我试图弄清楚如何围绕这个控制器的内容编写单元测试。目前我有以下几点:

describe('myApp', function () {
    var $scope;

    beforeEach(module('myApp'));
    beforeEach(inject(function ($controller, $rootScope) {
        $scope = $rootScope.$new();
    }));

    it('should create my directive', inject(function ($rootScope, $compile, $log) {
      var card = angular.element('<my-directive></my-directive>');
      $compile(card)($rootScope);
      $scope.$digest();

      expect($log.assertEmpty).not.toThrow();
    }));
});
description('myApp',函数(){
var$范围;
在每个模块之前(模块(‘myApp’);
beforeach(注入函数($controller,$rootScope){
$scope=$rootScope.$new();
}));
它('should create my directive',inject(函数($rootScope,$compile,$log){
var卡=角度元素(“”);
$compile(card)($rootScope);
$scope.$digest();
expect($log.assertEmpty).not.toThrow();
}));
});
上面的单元测试测试指令本身的创建。但是,我不知道如何在指令内部测试控制器。有办法做到这一点吗?如果是,怎么做


谢谢大家!

据我所知,有一种用于分隔指令控制器的语法,如:

.directive('myDirective', function() {
    return {
        restrict: 'E',
        transclude: true,
        replace: true,
        scope: {
          title:'=',
          state:'@'
        },
        templateUrl: 'myHtml.tpl.html',
        controller: 'MyDirectiveCtrl as ctrl'
    };
})
并将其注册为常规控制器

.controller("MyDirectiveCtrl", function($scope) {})

因此,如果您这样做,那么您可以使用$controller创建此控制器并愉快地进行测试。

您可以使用以下代码测试控制器:

describe('myApp', function () {
var $scope;

beforeEach(module('myApp'));
beforeEach(inject(function ($controller, $rootScope) {
    $scope = $rootScope.$new();
    var card = angular.element('<my-directive></my-directive>');
    $compile(card)($rootScope);
    $scope.$digest();
    controller = element.controller
}));

it('should create my directive', inject(function ($rootScope, $compile, $log) {
    //TRY YOUR BUSINESS LOGIC TESTS HERE
    //$scope.yourFunction(yourArgs...);
    //expect($scope.yourVars).toSomething
}));
description('myApp',函数(){
var$范围;
在每个模块之前(模块(‘myApp’);
beforeach(注入函数($controller,$rootScope){
$scope=$rootScope.$new();
var卡=角度元素(“”);
$compile(card)($rootScope);
$scope.$digest();
controller=element.controller
}));
它('should create my directive',inject(函数($rootScope,$compile,$log){
//在这里尝试您的业务逻辑测试
//$scope.yourFunction(yourArgs…);
//期待($scope.yourVars)做点什么
}));

}))

如果控制器正在注入服务,是否有方法对其进行测试?或者这仅适用于传递所有信息的“dumb”指令?它应该是controller=card.controller吗?