Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot spy on angular.element

I have one Jasmine test that is continously failing due to a spyOn not executing.

The following test will automatically fail:

it('simple test', function() {
    spyOn(angular, 'element');
});

The error is:

TypeError: 'undefined' is not an object (evaluating 'angular.element(handle.elem).off')
        at /Users/geoff/Project/www/components/angular-mocks/angular-mocks.js:1946
        at /Users/geoff/Project/www/components/angular-mocks/angular-mocks.js:1977

This error only seems to happen with angular.element. spying on other angular methods such as angular.copy and angular.forEach do not throw this error. I am using Jasmine 2.0 and Angular ~1.3. Any advice on fixing this problem would be appreciated.

like image 318
geoff Avatar asked Jul 27 '15 17:07

geoff


1 Answers

You need to allow access to the real object.

spyOn(angular, 'element').and.callThrough();

The code is trying to access a property on the return value, but the spy is not returning anything. You can't access .off on an undefined object!

like image 111
Preston Van Loon Avatar answered Sep 19 '22 00:09

Preston Van Loon