2013-04-18 51 views
2

我想测试下面的指令。AngularJS指令测试

angular.module('myModule.directives', []) 
    .directive('test', function() { 
    return { 
     restrictions: 'E', 
     scope: {}, 
     transclude: true, 
     replace: true, 
     template: '<div><ui><li>a</li></ul></div>' 
    } 
}); 

在我的测试:

describe('directives', function() { 
    var compile, 
    scope, 
    test; 

    beforeEach(module('myModule.directives')); 
    beforeEach(inject(function ($compile, $rootScope) { 

    compile = $compile; 
    scope = $rootScope; 
    test = angular.element('<test></test>'); 
    compile(test)(scope); 
    scope.$digest(); 

    it('should render the test directive', function() { 
     var lis = test.find('li'); 
     console.log(lis.length); // should output 1 
    }); 

})); 

测试失败,输出0。我很好奇,编译是否真正做任何事情。如果我有

test = angular.element("<test><li></li></test>");. 

那么“发现”确实发现测试元件,这正好说明编译内里没有呈现替代测试

'<div><ui><li>a</li></ul></div>' 

,因为它应该。有任何想法吗?我看了下面的视频(http://www.youtube.com/watch?v=rB5b67Cg6bc)。

回答

2

您需要将“限制”更改为“限制”

+0

我从未意识到这种错误。这并没有解决它。我也尝试输出到像你最初评论的变量。 – Apples

+0

适用于我... :) http://plnkr.co/edit/eikdTTzAF1hALF36a9fv?p=preview –

+0

@aaron你看过我制作的plunkr吗? –