How to Unit Test Isolated Scope Directive in AngularJS

See angular element api docs. If you use element.scope() you get the element’s scope that you defined in the scope property of your directive. If you use element.isolateScope() you get the entire isolated scope.
For example, if your directive looks something like this :

scope : {
 myScopeThingy : '='
},
controller : function($scope){
 $scope.myIsolatedThingy = 'some value';
}

Then calling element.scope() in your test will return

{ myScopeThingy : 'whatever value this is bound to' }

But if you call element.isolateScope() you’ll get

{ 
  myScopeThingy : 'whatever value this is bound to', 
  myIsolatedThingy : 'some value'
}

This is true as of angular 1.2.2 or 1.2.3, not sure exactly.
In previous versions you had only element.scope().

Leave a Comment