javascript - How to mock required directive controller in directive UT -


according angularjs doc a/the directive controller is:

instantiated before pre-linking phase , shared other directives if request name (see require attribute). allows directives communicate each other , augment each other's behavior.

this sounds great , useful in case ui view composed of container , widget, widget's link func can passed in container directive controller via declarative approach require:^cotnainerdirective. gives alternative way callback container behavior instead of communication relying on events.

for example, widget directive requiring container controller 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){                 ....             }         }     }; }]); 

the code inside link function additional work if widget living inside container. code working fine. however, when coming unit testing widget directive, hard think way send in mock container directive controller because not injected via angular $injector service.

probably, need write ut container perspective, somehow involves preparation work needed bootstrapping container directives. encounter before , can share points here?

it turns out if want unit test widget directive, has loosely decoupled container, in case using "require" not idea since making widget directive tightly dependents on container. have changed design use event-driven communication btw widget , container, in way can mock event listener watch on events sent widget directive in ut.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -