Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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!

share|improve this question
    
Try element.controller(''list"), see if you get the controller. – Chandermani Sep 29 at 11:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.