mercredi 24 février 2016

How to create unit test for directive in my case?

I am following John Papa's angular guide and I have a question regarding his codes.

http://ift.tt/1T8rW2w

angular
    .module('app')
    .directive('myExample', myExample);

function myExample() {
    var directive = {
        restrict: 'EA',
        templateUrl: 'app/feature/example.directive.html',
        scope: {
            max: '='
        },
        controller: ExampleController,
        controllerAs: 'vm',
        bindToController: true
    };

    return directive;
}

function ExampleController() {
    var vm = this;
    vm.min = 3;
    console.log('CTRL: vm.min = %s', vm.min);
    console.log('CTRL: vm.max = %s', vm.max);
}

<!-- example.directive.html -->
<div>hello world</div>
<div>max={{vm.max}}<input ng-model="vm.max"/></div>
<div>min={{vm.min}}<input ng-model="vm.min"/></div>

My question is how I can build unit test for his style.

describe('my directive', function() {
    var $controller;
    var scope;
    var $rootScope;
    var compile;
    var vm;

    beforeEach(module('app'));

    beforeEach(inject(function($injector) {
        $controller = $injector.get('$controller');
        $rootScope = $injector.get('$rootScope');
        compile = $injector.get('$compile');

        scope = $rootScope.$new();

        vm = $controller('ExampleController', {
            $scope: scope
        });
    }));

    it('should initialize controller', function() {
        var element = compile('<my-example></my-example>')(scope);
        more to test....
    });   
});

I am getting errors on 'ExampleController' is undefined. I am not sure how to perforce unit test for this style. Can anyone help me about it? Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire