我有以下指令:
'use strict';
angular.module('epicuro.ui.authorization')
.directive('ngIfRole', function(AuthorizationContext) {
return {
priority: 601,
restrict: 'A',
scope: true,
compile: function($element, $attr) {
if ($attr.ngIf) {
$attr.ngIf = '(' + $attr.ngIf + ') &&';
} else {
$attr.ngIf = '';
}
$attr.ngIf += 'hasRole()';
return function ($scope, $element, $attr) {
var value = $attr.ngIfRole;
var requiredRole = $scope.$eval(value);
$scope.hasRole = function () {
return AuthorizationContext.hasRole(requiredRole);
};
};
}
};
});
工作正常。当用户没有所需角色时,具有此属性的元素将被隐藏。但是现在我试图围绕它创建一个测试场景,我无法让它工作:
describe('The ngIfRole Directive', function () {
'use strict';
var $scope,
$element,
compileDirective,
AuthorizationContext = { hasRole: jasmine.createSpy('hasRole') };
beforeEach(module('epicuro.ui.authorization', function($provide) {
$provide.value('AuthorizationContext', AuthorizationContext);
}));
beforeEach(inject(function ($compile, $rootScope) {
$scope = $rootScope.$new();
compileDirective = function(html){
$element = angular.element(html);
$compile($element)($scope);
$scope.$apply();
};
}));
it('Calls the AuthorizationContext to see if the element should be displayed', function() {
AuthorizationContext.hasRole.and.returnValue(true);
compileDirective('<div ng-if-role="\'role\'"></div>');
expect(AuthorizationContext.hasRole).toHaveBeenCalledWith('role');
});
});
期望失败,说永远不会调用hasRole方法。如果我用&#39; ng-if =&#34; hasRole()&#34;&#39;替换ng-if-role属性,则测试通过,所以看起来整个编译阶段进展顺利。
知道我在这里做错了吗?
答案 0 :(得分:0)