【问题标题】:testing ng-transclude doesn't work测试 ng-transclude 不起作用
【发布时间】:2015-05-06 11:11:55
【问题描述】:

我正在编写两个指令来包装 ui-bootstrap 的 tabset 和 tab 指令。
为了将我的指令的内容传递给包装的指令,我在它们中都使用了嵌入。
这工作得很好,唯一的问题是我没有编写一个检查它的测试。我的测试使用替换指令作为包装指令的模拟,我在每次测试之前使用 $compileProvider 替换它。

测试代码如下所示:

beforeEach(module('myModule', function($compileProvider) {
    // Mock the internally used 'tab' which is a third party and should not be tested here
    $compileProvider.directive('tab', function() {
        // Provide a directive with a high priority and 'terminal' set to true, makes sure that
        // the mock directive will get executed, and that the real directive will not
        var mock = {
            priority: 100,
            terminal: true,
            restrict: 'EAC',
            replace: true,
            transclude: true,
            template: '<div class="mock" ng-transclude></div>'
        };

        return mock;
    });
}));

beforeEach(function() {
    inject(function(_$compile_, _$rootScope_) {
        $compile = _$compile_;
        $rootScope = _$rootScope_;
    });
});

beforeEach(function() {
    $scope = $rootScope.$new();
});

afterEach(function() {
    $scope.$destroy();
});

it('Places the enclosed html inside the tab body', function() {
    element = $compile("<div><my-tab>test paragraph</my-tab></div>")($scope);
    $scope.$digest();

    console.log("element.html() = ", element.html());

    expect(element.text().trim()).toEqual("test paragraph");
});

我的指令模板如下所示:

<div><tab><div ng-transclude></div></tab></div>

指令模块看起来像这样:

angular.module('myModule', ['ui.bootstrap'])

.directive('myTab', function() {
    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        templateUrl: 'templates/my-tab.tpl.html',

        scope: {
        }
    };
});

打印到控制台的结果是这样的:

LOG: 'element.html() = ', '<div class="ng-isolate-scope" id=""><div id="" heading="" class="mock"><ng-transclude></ng-transclude></div></div>'

关于为什么不发生嵌入的任何想法(同样,它在测试之外也可以正常工作)?

更新

我已经转向其他事情和指令,并再次遇到这个问题,但现在更重要的是,我放置在父指令中的指令在其链接功能中需要父控制器.

我对此进行了更多研究,结果发现由于某种原因,编译 mock 指令不会创建嵌入内容的实例。
我知道这一点的原因是,我在两个指令(模拟和嵌入指令)的每个钩子中都放置了一个打印输出,即编译、预链接、后链接和控制器构造函数,我看到只有打印输出来自模拟指令。

现在,真正有趣的部分来了:我尝试在模拟指令的链接函数中使用 transclude 函数来“强制”编译 transcluded 指令,它奏效了! (另一个证明它不是隐式发生的)。
你问的问题在哪里?好吧,它仍然不起作用。这一次,由于 transcluded 指令的链接功能失败,因为它没有找到模拟指令的控制器。什么?!

代码如下:

代码

var mod = angular.module('MyModule', []);

mod.directive('parent', function() {
    return {
        restrict: 'E',
        replace: true,
        template: '<div class="parent">...</div>',

        controller: function() {
            this.foo = function() { ... };
        }
    };
});

mod.directive('child', function() {
    return {
        restrict: 'E',
        require: '^parent',        

        link: function(scope, element, attrs, parentCtrl) {
            parentCtrl.foo();
        }
    };
});

测试

describe('child directive', function() {
    beforeEach(module('MyModule', function($compileProvider) {
        $compileProvider.directive('parent', function() {
            return {
                priority: 100,
                terminal: true,
                restrict: 'E',
                replace: true,
                transclude: true,

                template: '<div class="mock"><ng-transclude></ng-transclude></div>',

                controller: function() {
                    this.foo = jasmine.createSpy();
                },

                link: function(scope, element, attrs, ctrls, transcludeFn) {
                    transcludeFn();
                }
            };
        });
    }));
});

此测试失败并显示如下错误消息:

错误:[$compile:ctreq] 控制器“父级”,指令要求 '孩子',找不到!

任何想法、想法、建议都将受到高度赞赏。

【问题讨论】:

  • 您可以尝试在 URL 前添加正斜杠,如下所示:templateUrl: '/templates/my-tab.tpl.html'
  • 这绝对不是问题,因为找到了模板(否则模拟不会在控制台输出中可见,指令模板就是在那个 HTML 中植入模拟的那个)。开头没有斜线的原因是该文件夹相对于 JS 文件所在的位置。无论如何,这无关紧要。

标签: angularjs unit-testing angularjs-directive mocking jasmine


【解决方案1】:

好吧,这可能是 SO 历史上最短的赏金......

问题在于模拟指令的terminal: truepriority: 100 属性。我的印象是(来自我在网上阅读的一篇关于如何模拟指令的文章),这些属性会导致编译器停止编译具有相同名称的指令并优先考虑首先评估的模拟指令。
我显然错了。查看thisthis,很明显:

  1. 'terminal' 停止处理任何其他尚未处理的指令
  2. 'priority' 用于确保模拟指令在它正在模拟的指令之前被处理

问题在于,这会导致所有其他处理停止,包括ng-transclude 指令,其默认优先级为0

但是,删除这些属性会导致一切崩溃,因为这两个指令都已注册,等等(我不会用所有血淋淋的细节给你带来负担)。为了能够删除这些属性,这两个指令应该位于不同的模块中,并且它们之间应该没有依赖关系。简而言之,在测试 child 指令时,唯一评估的名为 parent 的指令应该是模拟指令。
为了支持现实生活中的使用,我在系统中引入了三个模块:

  • child 指令的模块(无依赖关系)
  • parent 指令的模块(无依赖关系)
  • 一个没有内容,但同时依赖于 childparent 模块的模块,这是您唯一需要作为依赖项添加到代码中的模块

差不多就是这样。我希望它可以帮助遇到此类问题的其他人。

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多