5

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.

geoff
  • 2,251
  • 1
  • 19
  • 34

1 Answers1

6

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!

Preston Van Loon
  • 1,061
  • 6
  • 11