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

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

问题描述

根据 AngularJS 文档,指令控制器是:

According to AngularJS doc a/the directive controller is:

在预链接阶段之前实例化,如果其他指令按名称请求,则与其他指令共享(请参阅 require 属性).这允许指令相互通信并增强彼此的行为.

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.

这在 UI 视图由容器和小部件组成的情况下听起来很棒且有用,小部件的链接函数可以通过声明性方法在容器指令控制器中传递require:^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){
                ....
            }
        }
    };
}]);

如果小部件位于容器内,则链接函数内的代码将执行额外的工作.代码工作正常.然而,当对小部件指令进行单元测试时,很难想到一种发送模拟容器指令控制器的好方法,因为它不是通过 Angular $injector 服务注入的.

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,但这不知何故涉及引导容器指令所需的太多准备工作.任何人以前遇到过这种情况,可以在这里分享一些好处吗?

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?

推荐答案

事实证明,如果我想对小部件指令进行单元测试,它必须与容器松散地解耦,在这种情况下使用require"不是一个好方法想法,因为它实际上使小部件指令紧密依赖于容器.我已经改变了我的设计以使用事件驱动的通信 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天全站免登陆