jeudi 29 octobre 2015

Testing directives with controller

I have a directive with a controller:

aaaApp.directive('aaaButton', function() {
    return {
        restrict: 'E',
        scope : {
            id: '@',
            valueKey: '@value',
            label: '@'
        },
        controller: 'ButtonController',
        template:
        '<div id="{{id}}" class="aaa-button-default"> ' +
        '<button ng-click="doClick(valueKey)">{{label}}</button>' +
        '</div>'
    };
});

And i want to access to the scope functions for testing, but for now i am testing just with vars of the scope in the controller. I have pot a variable on the scope:

controller('ButtonController', function ($scope, WSService) {
    $scope.testing = "hello";
})

Im tryng to test if $scope.testing is equals to "hello" on the generated controller when the directive is compiled:

'use strict';
describe('directives', function() {
    var $scope, controller, template;
    beforeEach(module('aaaApp'));
    beforeEach(inject(function($rootScope, $compile) {
        $scope = $rootScope.$new();
        var element = angular.element('<aaa-button id="1234" value="cancel" label="Cancel"></aaa-button>');
        template = $compile(element)($scope);
        $scope.$digest();
    }));
    it("testing controller", inject(function() {
        expect($scope.testing).toBe("hello");
    }));
});

The problem is that im geting undefined on the $scope.testing so i cant acces to the $scope of the controller.

Aucun commentaire:

Enregistrer un commentaire