我有使用隔离范围和控制器的指令,是否正确编写指令的单元测试并在控制器范围内测试某些功能?
如果是的话,我怎样才能访问控制器范围?
angular.module('myApp').directive('myDirecive', function () {
return {
templateUrl: 'template.html',
scope: {
data: '='
},
controller: function ($scope) {
$scope.f= function () {
$scope.value = true;
};
},
link: function ($scope) {
// some logic
});
}
};
})
describe('myDirective', function () {
'use strict';
var element,
$compile,
$rootScope,
$scope;
beforeEach(module('myApp'));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
element = compileElement();
}));
function compileElement() {
var element = angular.element('<my-directive></my-directive>');
var compiledElement = $compile(element)($scope);
$scope.$digest();
return compiledElement;
}
it('should execute f', function () {
$scope.f();
expect($scope.val).toBe(true); // there we can access to isolate scope from directive;
});
});
答案 0 :(得分:2)
控制器被赋予指令创建的隔离范围。您传递给编译函数的$scope
用作指令的隔离范围的父级。
回答如何测试它,你有两个选择:
从元素中访问隔离范围:
var isolated = element.isolateScope();
为此,您需要$compileProvider.debugInfoEnabled(true);
从您的范围访问隔离范围:
var isolated = $scope.childHead;
答案 1 :(得分:0)
试试这个
first initialize string reversed with empty string i.e string reversed="";
after this change for loop:
for(int i=0;i<length;i++)
{
reversed+=for_reversal[length-1-i];
cout << for_reversal[length-i] << endl;
}
int r_length=reversed.length();
cout << "reversed string length : " << r_length << endl;
cout << "Reversed string is : " << reversed << endl;
看看这个Article