Get the instance of the directive controller with Angular / Jasmine

advertisements

When unit testing angular directives, how can I grab an instance of the directive controller and assert certain data bindings on the controller?

function myDirective() {
  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'tpl.html',
    scope: { },
    controller: function($scope) {
      $scope.text = 'Some text';
    }
  };
}

angular
  .module('example')
  .directive('myDirective', [
    myDirective
  ]);

unit test

    describe('<my-directive>', function() {
      var element;
      var $compile;
      var $scope;

      beforeEach(module('example'));

      beforeEach(function() {
        inject(function(_$compile_, _$rootScope_) {
          $compile = _$compile_;
          $scope = _$rootScope_.$new();
        });
      });

      describe('basic functionality', function() {
        beforeEach(function() {
          element = $compile('<my-directive></my-directive')($scope);
          $scope.$digest();
        });

        it('should bind the correct text', function() {
          //?
        });
      });
    });


element = $compile('<my-directive></my-directive')($scope);
$scope.$digest();
ctrl = element.controller('myDirective');