jeudi 29 octobre 2015

Unit testing directive's controller

I am struggling to get the controller from within a directive for unit testing. Here is my angular app:

angular.module('test-app', [])

    .controller('loadingCtr', ['$scope', function ($scope) {

    }])

    .directive('loading', function() {
        return {
            restrict: 'E',
            controller: 'loadingCtr'
       };
    });

Here is my unit test code:

describe('loading', function () {

    beforeEach(inject(function($rootScope, $compile) {

        var fooElement = $compile('<loading></loading>')($rootScope);
        var fooController = fooElement.controller('loading');

        $rootScope.$digest();
        console.log(fooController);
    })); 

    describe('loading: testing loading directive', function() {
        it('should create loading directive', function() {
        });
    });
});

Here is a plnkr to mess around with: http://ift.tt/1NbjWKb

fooController always returns as undefined. I've tried using the following examples I've found online, but I always get the same results:

Unit testing a directive that defines a controller in AngularJS

http://ift.tt/1NbjWKd

Is there something obvious here that I am missing?

Aucun commentaire:

Enregistrer un commentaire