我有accordionPanel
父指令require
的控制器的AngularJs指令accordion
。我需要测试accordionPanel
指令,以便在调用foldUnfold
函数时查看模型是否发生更改。如何编写单元测试以查看模型是否在foldUnfold
调用时更改。这是我的指令的简化版本和我到目前为止的测试是在下面:
.directive("accordion", [
function() {
return {
templateUrl: "otherurl",
transclude: true,
replace: true,
scope: {
},
controller: ["$scope",function($scope) {
this.isOneOpenOnly = function() {
return $scope.oneOpenOnly;
}
}],
link: function(scope, elem, attrs, ctrl, linker) {
// some code
}
}
}
])
.directive("accordionPanel", [
function() {
return {
templateUrl: "urlblah",
transclude: true,
replace: true,
require: "^accordion",
scope: {},
link: function(scope, elem, attrs, ctrl, linker) {
scope.foldUnfold = function() {
// some logic here then
scope.changeThisModel=ctrl.isOneOpenOnly();
}
}
}
}
])
到目前为止我的测试:
it('Should return unfolded as true', function() {
var scope=$rootScope.$new(),
element=$compile("<div accordion><div accordion-panel></div></div>")(scope);
scope.$digest();
scope.foldUnfold(); // this is fails as scope refers to accordion but I need to access accordionPanel
expect(scope.changeThisModel).toBe(true);
});
问题是我无法访问accordionPanel
所在的foldUnfold
范围。我认为可以通过$$childHead
等方式访问它,但即使可能,它似乎也不是正确的方法。那我怎么测试呢?