mardi 29 septembre 2015

Testing angular js directive's inline controller method using Jasmine?

I facing difficulty in covering the inline controller function defined in a directive using Jasmine and Karma. Below is the code of that directive.

myApp.directive("list", function() {
    return {
        restrict: "E",
        scope: {
            items: "=",
            click: "&onClick"
        },
        templateUrl: "directives/list.html",
        controller: function($scope, $routeParams) {
            $scope.selectItem = function(item) {
                $scope.click({
                    item: item
                });
            }
        }
    }     
});

Unit Test case is below:

describe('Directive:List', function () {

    var element, $scope, template, controller;
    beforeEach(inject(function ($rootScope, $injector, $compile, $templateCache, $routeParams) {
        $httpBackend = $injector.get('$httpBackend');
        $httpBackend.when("GET", "directives/list.html").respond({});
        $scope = $rootScope.$new();
        element = $compile('<list></list>')($scope);
        template = $templateCache.get('directives/list.html');
        $scope.$digest();
        controller = element.controller($scope, $routeParams);
    }));

    it('Test Case-1: should contain the template', function () {
        expect(element.html()).toMatch(template);
    });
}); 

In my code coverage report, the controller function is not covered. Also I am not able to get the controller instance and test the selectItem method.

Any idea would be of great help!

Aucun commentaire:

Enregistrer un commentaire