所以我确实看到了另一个问题:如何在指令 UT 中模拟所需的指令控制器,这基本上是我的问题,但似乎这个线程的答案是“改变你的设计”。我想确保没有办法做到这一点。我有一个指令,它声明了一个由子指令使用的控制器。我现在正在尝试为 children 指令编写 jasmine 测试,但我无法让它们在测试中编译,因为它们依赖于控制器。这是它的样子:
addressModule.directive('address', ['$http', function($http){
return {
replace: false,
restrict: 'A',
scope: {
config: '='
},
template: '<div id="addressContainer">' +
'<div ng-if="!showAddressSelectionPage" basic-address config="config"/>' +
'<div ng-if="showAddressSelectionPage" address-selector addresses="standardizedAddresses"/>' +
'</div>',
controller: function($scope)
{
this.showAddressInput = function(){
$scope.showAddressSelectionPage = false;
};
this.showAddressSelection = function(){
$scope.getStandardizedAddresses();
};
this.finish = function(){
$scope.finishAddress();
};
},
link: function(scope, element, attrs) {
...
}
}
}])
子指令:
addressModule.directive('basicAddress360', ['translationService', function(translationService){
return {
replace: true,
restrict: 'A',
scope: {
config: '='
},
template:
'...',
require: "^address360",
link: function(scope, element, attrs, addressController){
...
}
}
}])
茉莉花测试:
it("should do something", inject(function($compile, $rootScope){
parentHtml = '<div address/>';
subDirectiveHtml = '<div basic-address>';
parentElement = $compile(parentHtml)(rootScope);
parentScope = parentElement.scope();
directiveElement = $compile(subDirectiveHtml)(parentScope);
directiveScope = directiveElement.scope();
$rootScope.$digest();
}));
我有没有办法用茉莉花测试子指令,如果是这样,我错过了什么?即使我可以在没有控制器功能的情况下测试指令本身,我也会很高兴。