I'm trying to test the controller of a parent directive, but I can't figure
out where to get a copy of the controller instance. The parent directive
looks like the following:
( function()
{
angular.module('app').directive('parentDirective', parentDirective);
function parentDirective()
{
var directive = {
scope: {
'elementId': '@'
},
restrict: 'A',
controller: parentDirectiveController
}
return directive;
}
function parentDirectiveController()
{
var vm = this;
vm.someFunc = someFunc;
function someFunc()
{
...
}
}
}());
In my unit tests I can't find a way to grab the instance of
parentDirectiveController for testing without modifying the directive
definition. The ways I "could" test are
1. Add controllerAs: 'Ctrl' to the directive definition and access it
through element.isolateScope().Ctrl
2. Test through the child directive that gets passed a copy of the
parents controller
3. Change the controller to add someFunc to $scope and just access it
through the scope in the test
4. Make the parentDirectiveController a full controller by calling
angular.controller() and using $controller() in the unit tests
Option 1 is the easiest, but I feel it makes me add something to the
directive just to facilitate testing since the function won't be used in
any template and adds a coupling of the tests to the scope variable name.
Option 2 is possible, but then I can't test the controller in isolation
Option 3 I don't want to even entertain as I am trying to get away from
using $scope directly
Option 4 is easy, but again I have to add something to facilitate testing
when this controller will never be used with ngController.
When angular instantiates the directive, it must new up the controller and
store it somewhere to pass to child directives. Is this location accessible
to grab a copy of the instance for testing, or am I stuck with one of the
four options above?
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.