AngularJS指令测试

AngularJS指令测试,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在尝试测试以下指令 angular.module('myModule.directives', []) .directive('test', function() { return { restrictions: 'E', scope: {}, transclude: true, replace: true, template: '<div><ui><li>a</li>&l

我正在尝试测试以下指令

angular.module('myModule.directives', [])
  .directive('test', function() {
    return {
      restrictions: 'E',
      scope: {},
      transclude: true,
      replace: true,
      template: '<div><ui><li>a</li></ul></div>'
    }
});
angular.module('myModule.directives',[])
.directive('test',function(){
返回{
限制:“E”,
作用域:{},
是的,
替换:正确,
模板:“
  • a
  • ” } });
    在我的测试中:

    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
        });
    
    }));  
    
    description('directives',function(){
    变量编译,
    范围
    试验;
    在每个(模块('myModule.directions')之前;
    beforeach(注入函数($compile,$rootScope){
    compile=$compile;
    scope=$rootScope;
    测试=角度元素(“”);
    编制(测试)(范围);
    范围。$digest();
    它('应该呈现测试指令',函数(){
    var lis=测试。查找('li');
    console.log(lis.length);//应输出1
    });
    }));  
    
    测试失败并输出0。我很好奇编译是否真的在做任何事情。如果我用

      test = angular.element("<test><li></li></test>");. 
    
    test=angular.element(“
  • ”);。
    然后“find”确实在测试元素中找到了li,这表明compile没有呈现给

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

  • 应该如此。有什么想法吗?我看了下面的视频作为指导()。

    你需要将“限制”改为“限制”

    我从未意识到这个错误。这并没有解决它。我还尝试输出到一个变量,就像你最初评论的那样。@aaron你看了我做的plunkr了吗?感谢你设置了这个例子。当我弄清楚为什么它在我的环境中不起作用时,我会在这里发布。再次感谢。