Unable to test unit Angular directive that uses $ window

I have a bunch of test cases for various Angular directives (1.4.7), and I use Karma, Jasmine and Sinon for testing.

I am trying to add unit test for a new directive, which is the only directive in which I am currently using $window , but I see a cryptic error in the console output:

TypeError: 'undefined' is not an object (rating 'this.proxy.toString')

This error comes from sinon.js on line 2372.

I do all the β€œnormal” things in the unit test directive, for example, creating a fake element with the directive as an attribute:

 testElement = document.createElement('div'); testElement.setAttribute('data-my-directive'); document.body.appendChild(testElement); 

And compilation of the directive:

 $compile(testElement)($scope); 

I use $provide to try the mock $window object:

 module('app', function ($provide) { $provide.value('$window', { id: 'test' }); }); 

But as soon as I try to use $window in the test file, the error shown above is thrown.

As I said, I have a bunch of other unit tests for other directives, services, and controllers working as expected, so everything seems to be configured correctly. This is exactly this test.

Any ideas?

+7
javascript angularjs karma-runner sinon
source share
1 answer

I'm not sure if this is the same error, but just a couple of days ago, a fix for a similar problem was solved on sinon github:

https://github.com/sinonjs/sinon/pull/833

The fix contains the lines:

 var callStr = this.proxy ? this.proxy.toString() + "(" : ""; 

where the null check is one and several other lines.

This fix is ​​located in the lib/sinon/call.js in commit 7a18eb5 .

I am not sure if this is the same because the file is different as well as the line. However, it was so interesting that I will try the latest version of Sinon and see if it fixes. It is possible that a similar error occurs in several parts of the synon if, for example, the encoder is the same in both files.

+2
source share

All Articles