Anup
Anup

Reputation: 9738

Angularjs Unit Test - function to be defined

How do i check whether a particular function is defined or not?

I am using following unit test, doesn't work.

controller

$scope.filterData = function () {
        $scope.currentPage = "1";
        $scope.displayData($scope.currentPage);
        ......
    }

test

var ctrl, scope;

beforeEach(function () {
    // load the module
    module('myApp');
    // inject Controller for testing
    inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        ctrl = $controller('DataCtrl', { $scope: scope });
    });
});
it('should have a filterData function to be defined', function () {
    expect(scope.filterData()).toBeDefined();
});

Upvotes: 0

Views: 2534

Answers (1)

David Losert
David Losert

Reputation: 4802

Remove the braces like this:

it('should have a filterData function to be defined', function () {
   expect(scope.filterData).toBeDefined();
});

Since you dont wanna execute the function itself, but only check if its there.

Upvotes: 2

Related Questions