FrankyBoy FrankyBoy - 1 year ago 47
AngularJS Question

Testing code that uses document.body.querySelectorAll

Trying to test a directive that does the following:

var inputs = angular.element(document.body.querySelectorAll('input[ng-model]', elem));
// [code relying on found elements]

Running inside karma/jasmine/phantomjs, this fails because it seems that
returns the document that contains the test, rather than the compiled template. Is there some way to mock this functionality so it works as expected (for my use case) or some other way to query for those elements?

PS: The elements that need to be located are in no known relation to the element that the directive is applied to.

Answer Source

You can use $document instead of document then mock it in your tests. See Angular js unit test mock document to learn how to mock $document.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download