編集:テストがすでに失敗した後にディレクティブのコンパイルが行われるようです。
テスト: ディレクティブに必要なサービス関数の完全なモックを提供しました。(ブレークポイントを追加して) デバッグモードでテストを実行すると、すべてが正しいように見えます。これまでに試したこと: ディレクティブの優先順位を下げる、コンパイルの代わりに $digest を訴える、その他多数。
describe('restrict', function(){
var scope, compile, html, elem, authService;
html = '<span data-restrict data-access="admin"></span>';
beforeEach(function(){
module('myApp.directives');
module('myApp.services');
inject(function($compile, $rootScope, $injector){
authService = $injector.get('authService');
authService.setRole('guest');
scope = $rootScope.$new();
compile = $compile;
});
});
it('should allow basic role-based content discretion', function(){
expect(elem.length).toEqual(0);
console.log(elem);//HTML node
timeout(function(){
console.log(elem);//undefined
}, 500);
});
});
指令:
angular.module('myApp.directives', []).
directive('restrict', function(authService){
return{
restrict: 'A',
prioriry: 100000,
scope: false,
compile: function(element, attr, linker){
debugger;//The test has already failed one I reach this break point!
var accessDenied = true;
var user = authService.getUser();
var attributes = attr.access.split(" ");
for(var i in attributes){
if(user.role == attributes[i]){
accessDenied = false;
}
}
if(accessDenied){
angular.forEach(element.children(), function(elm){
try{
elm.remove();
}
catch(ignore){}
});//TODO: find a better solution for IE or remove this code
element.children().remove();
element.remove();
}
}
}
});
テスト出力:
Chrome 30.0.1599 (Linux) restrict should allow basic role-based content discretion FAILED
Expected { 0 : HTMLNode, length : 1 } to equal 0.
Error: Expected { 0 : HTMLNode, length : 1 } to equal 0.
at null.<anonymous> (/var/www/front/dev/angular-seed/test/unit/directivesSpec.js:19:22)