2013-06-18 41 views
2

我有一个相当复杂的角度指令,称hy-plugin-window,我的ng-repeat块(complete directive code here)内使用:测试角果报:如何设置范围

<div ng-repeat="pluginD in pluginsDisplayed"> 
    <div hy-plugin-window content="pluginD" remove="remove"></div> 
</div> 

的指令使用的分离范围,至极是双向链接到父的content成员:

return { 
     restrict: 'A', 
     replace: true, 
     link: linker, 
     scope: { 
      content:'=', 
     } 
    }; 

这意味着,在创建时,列表中的每个指令都可以访问它的独特的内容(这是对象的单个成员)并做类似的事情:

scope.statusMessage = 'Loading ' + scope.content.name + '...'; 

一切正常,但我不知道如何测试它与噶玛。有了这样我希望一个相当普遍的测试手动设置范围的指令内:

'use strict'; 

describe('Directive: hyPluginWindow', function() { 

    beforeEach(module('hyacinthHostApp')); 

    var element, $compile, scope; 

    beforeEach(inject(function(_$compile_, $rootScope) { 
     $compile = _$compile_; 
     scope = $rootScope.$new(); 
    })); 


    it('should do something', inject(function() { 

     scope.content = { 
      name: "testElement", 
      id: "testElement000", 
      uiType: "canvas" 
     }; 

     element = angular.element('<div hy-plugin-window></div>'); 
     element = $compile(element)(scope); 

     //expect(...); 
    })); 
}); 

但悲惨的失败:

TypeError: Cannot read property 'name' of undefined 

第一次我的指令试图访问其scope.content.name

考虑到我是一个测试完整的新手,我错过了什么?

回答

3

小故事:将编译的元素更改为<div hy-plugin-window content="content"></div>,我认为它应该起作用。

该指令具有ISOLATE范围,这意味着它不会从“常规”范围原型继承。

在您的测试中,您正在定义范围上定义content,但指令本身已封装 - 它具有隔离范围,因此看不到在常规范围上定义的content属性。

您需要通过组件的显式接口进行通信,即content属性。

+0

来自Vojta Jina的回答!棒极了。 – James