简体   繁体   English

如何在Angular 1.x中模拟嵌套指令(单元测试)

[英]How to mock nested directive in Angular 1.x (Unit testing)

Hey I have problem with mock my directive. 嘿,我在模拟指令时遇到问题。 Look at this code: 看下面的代码:

 <dir-one data="data">
<div ng-repeat="e in data">
  <div ng-repeat="e2 in e">
   <dir-two prop="e2.some" prop2="e2.some">  <!-- i want to mock this directive -->
   </dir-two>
 </div>
</dir-one>

Not I want to mock this template to jasmine and reproduce dir-two click event. 我不是要模拟此模板来茉莉花并重现dir-two click事件。

I tryed something like this : 我尝试了这样的事情:

var tpl = `<dir-one data="data">
<div ng-repeat="e in data">
  <div ng-repeat="e2 in e">
   <dir-two prop="e2.some" prop2="e2.some">  <!-- i want to mock this directive -->
   </dir-two>
 </div>
</dir-one>`

    var newScope = $rootScope.$new();
    newScope.DATA = // some data;

    el= angular.element(tpl);
    $compile(el)(newScope);
    $rootScope.$digest();
    catchController = el.controller('dirTwoController'); // undefined
    catchController2 = el.controller('dirOneController'); // no error, catch object

There is a problem, becouse I cant reproduce dirTwoController using this mock, just dirOneController corectly showing output. 有一个问题,因为我无法使用此模拟来复制dirTwoController,只是dirOneController核心显示输出。

Any ideas for solve this problem ? 有解决这个问题的想法吗?

Thanks ! 谢谢 !

One way to do this would be to use $provide to mock the directive implementation. 一种方法是使用$provide模拟指令实现。 This could be done in beforeEach before the parent directive is compiled. 这可以在编译父指令之前在beforeEach中完成。

beforeEach(module(function ($provide) {
    $provide.factory('dirTwoDirective', function() { 
        return {
            // Implementation of directive here
        }; 
    });
}));

Note how the word Directive has been added to the end of the directive name. 请注意如何将“ 指令 ”一词添加到指令名称的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM