如何在嘲弄UT指令所需的指令控制器 [英] How to mock required directive controller in directive UT

查看:145
本文介绍了如何在嘲弄UT指令所需的指令控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据AngularJS DOC A /指令控制器是:

According to AngularJS doc a/the directive controller is:

在pre联阶段之前实例化,如果他们提出要求的名字(见要求属性)它与其他指令共享。这使得指令来彼此通信和增强彼此的行为。

instantiated before the pre-linking phase and it is shared with other directives if they request it by name (see require attribute). This allows the directives to communicate with each other and augment each other's behavior.

这听起来不错,并在用户界面视图是由容器和小部件的情况下是有用的,widget的链接FUNC可以通过声明的方式容器指令控制器通过要求:^ cotnainerDirective 。这给出了一个替代的方式来回调容器的行为,而不是通信依赖于事件。

This sounds great and useful in a case where UI view is composed of container and widget, widget's link func can be passed in the container directive controller via declarative approach require:^cotnainerDirective. This gives an alternative way to callback container behavior instead of communication relying on events.

例如,它是如下要求容器控制器插件指令:

For example, a widget directive which is requiring container controller as below:

angular.module('platform').directive('widget', [ function ( ) {
    return {
        restrict: 'E',
        transclude: true,
        require: '?^container',
        replace: true,
        scope: {
            layout: '=',
            model: '='
        },
        templateUrl: 'js/modules/platform/templates/form-tmpl.html',
        link: function (scope, element, iAttrs, requiredCtrl) {
            if(requiredCtrl && requiredCtrl.fooMethod){
                ....
            }
        }
    };
}]);

如果小部件是生活的容器内链接功能里面的code会做额外的工作。在code是工作的罚款。然而,来到单元测试小部件指令时,很难把它用到一个模拟的容器指令控制器发送,只是因为它不是通过角$注射器注射服务的好方法。

the code inside link function will do additional work if the widget is living inside a container. The code is working fine. However, when coming to unit testing the widget directive, it is hard to think about a good way to send in a mock container directive controller simply because it is not injected via Angular $injector service.

也许,我需要从容器的角度写的UT,但不知何故涉及需要提供引导容器指令太多preparation工作。之前任何人都遇到这个,可以在这里分享一些好点?

Probably, I need to write the UT from container perspective, but this somehow involves too much preparation work needed for bootstrapping the container directives. Anyone encounter this before and can share some good points here?

推荐答案

事实证明如果我要进行单元测试的小部件的指令,它使用规定是不是一个好必须从容器中松散分离,在这种情况下,因为它的想法实际上是使得小部件的指令在容器上紧紧家属。我已经改变了我的设计中使用事件驱动的通信BTW小部件和集装箱,这样,我可以简单地模拟事件监听器上观看由小部件在指令发送UT的事件。

It turns out if I want to unit test the widget directive, it has to be loosely decoupled from container, in that case using "require" is not a good idea since it is actually making the widget directive tightly dependents on the container. I have changed my design to use event-driven communication btw widget and container, in this way i can simply mock event listener to watch on the events sent by widget directive in UT.

这篇关于如何在嘲弄UT指令所需的指令控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆