geoff
geoff

Reputation: 2276

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.

Upvotes: 5

Views: 2641

Answers (1)

Preston Van Loon
Preston Van Loon

Reputation: 1060

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!

Upvotes: 6

Related Questions