Unit Test Directive Controllers in AngularJS

user3111277

I'm trying to fully understand how to test directives in AngularJS with Jasmine. Currently, I have a directive that is setup as shown here:

.directive('myDirective', function() {
    return {
        restrict: 'E',
        transclude: true,
        replace: true,
        scope: {
          title:'=',
          state:'@'
        },
        templateUrl: 'myHtml.tpl.html',
        controller: function ($scope) {
          // Business Logic Specific to the Directive goes in here
        }
    };
})

As the code snippet above shows, my directive has a controller inside the directive definition. The specific directive I'm trying to test is fairly complicated. The controller in the directive has over 200 lines of code. I'm trying to figure out how to write unit tests around the contents of the this controller. Currently I have the following:

describe('myApp', function () {
    var $scope;

    beforeEach(module('myApp'));
    beforeEach(inject(function ($controller, $rootScope) {
        $scope = $rootScope.$new();
    }));

    it('should create my directive', inject(function ($rootScope, $compile, $log) {
      var card = angular.element('<my-directive></my-directive>');
      $compile(card)($rootScope);
      $scope.$digest();

      expect($log.assertEmpty).not.toThrow();
    }));
});

The unit test above tests the creation of the directive itself. However, I can't figure out how to test the controller inside of the directive. Is there a way to do this? If so, how?

Thank you!

idursun

As far as I remember there is a syntax for separating controller of a directive like:

.directive('myDirective', function() {
    return {
        restrict: 'E',
        transclude: true,
        replace: true,
        scope: {
          title:'=',
          state:'@'
        },
        templateUrl: 'myHtml.tpl.html',
        controller: 'MyDirectiveCtrl as ctrl'
    };
})

and registering it as a regular controller

.controller("MyDirectiveCtrl", function($scope) {})

So if you do this then you can create this controller by using $controller and test happily.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unit Test Directive Controllers in AngularJS

From Dev

Angularjs directive unit test issue

From Dev

AngularJS - Unit Test Directive with jquery function

From Java

How to Unit Test Isolated Scope Directive in AngularJS

From Dev

Writing unit test for AngularJS directive with Jasmine?

From Dev

Programmatically clicking an <A> from unit test in AngularJS directive

From Dev

Undefined function in AngularJS directive unit test

From Dev

Unit testing controllers in angularJS

From Dev

Unit Test Angularjs directive, which contains private timeout, with Jasmine

From Dev

AngularJS unit test for directive that queries server returns "Unsatisfied requests"

From Dev

Unit test AngularJS directive with template containing ng-if

From Dev

Angularjs unit test resolve promise inside custom directive with external template

From Dev

Unit Test Angularjs directive, which contains private timeout, with Jasmine

From Dev

AngularJS directive with require and 2 controllers

From Dev

Use directive with different controllers in AngularJS?

From Dev

Unit testing promises in controllers in AngularJS

From Dev

Unit test $formatters in Angular directive

From Dev

Unit Test Directive with Isolated Scope

From Dev

Unit test $formatters in Angular directive

From Dev

AngularJS Unit testing a link in a directive

From Dev

Using different controllers with custom directive in AngularJS?

From Dev

Unit test of controller angularjs

From Dev

AngularJS - Unit Test with $timeout

From Dev

Unit test angularjs controller

From Dev

AngularJs unit test 'this'

From Dev

Angularjs Unit Test for Service

From Dev

How can I set an element's height in an AngularJS unit test for a directive?

From Dev

Unit Test controllers in Play 2 framework with Scala

From Dev

SailsJS: How to properly unit test controllers?

Related Related

  1. 1

    Unit Test Directive Controllers in AngularJS

  2. 2

    Angularjs directive unit test issue

  3. 3

    AngularJS - Unit Test Directive with jquery function

  4. 4

    How to Unit Test Isolated Scope Directive in AngularJS

  5. 5

    Writing unit test for AngularJS directive with Jasmine?

  6. 6

    Programmatically clicking an <A> from unit test in AngularJS directive

  7. 7

    Undefined function in AngularJS directive unit test

  8. 8

    Unit testing controllers in angularJS

  9. 9

    Unit Test Angularjs directive, which contains private timeout, with Jasmine

  10. 10

    AngularJS unit test for directive that queries server returns "Unsatisfied requests"

  11. 11

    Unit test AngularJS directive with template containing ng-if

  12. 12

    Angularjs unit test resolve promise inside custom directive with external template

  13. 13

    Unit Test Angularjs directive, which contains private timeout, with Jasmine

  14. 14

    AngularJS directive with require and 2 controllers

  15. 15

    Use directive with different controllers in AngularJS?

  16. 16

    Unit testing promises in controllers in AngularJS

  17. 17

    Unit test $formatters in Angular directive

  18. 18

    Unit Test Directive with Isolated Scope

  19. 19

    Unit test $formatters in Angular directive

  20. 20

    AngularJS Unit testing a link in a directive

  21. 21

    Using different controllers with custom directive in AngularJS?

  22. 22

    Unit test of controller angularjs

  23. 23

    AngularJS - Unit Test with $timeout

  24. 24

    Unit test angularjs controller

  25. 25

    AngularJs unit test 'this'

  26. 26

    Angularjs Unit Test for Service

  27. 27

    How can I set an element's height in an AngularJS unit test for a directive?

  28. 28

    Unit Test controllers in Play 2 framework with Scala

  29. 29

    SailsJS: How to properly unit test controllers?

HotTag

Archive