javascriptunit-testingangularjsfile-uploadxmlhttprequest-level2

Can I test regular XMLHttpRequests with Angular's $httpBackend?


I have an AngularJS application, which needs file uploads, and as $http does not support the special features of XMLHttpRequest.upload, I have to perform the file upload using an XMLHttpRequest.

My question is: Can I use the $httpBackend to unit test the requests made via new XMLHttpRequest()?

I have some tests made, but at the moment $httpBackend is throwing errors. Without calling $httpBackend.flush() it complained about unsatisfied requests, but adding the flush, just caused an "No pending requests" error.

Extensive use of search engines didn't yield an answer for me, so can you help me?


Solution

  • I suspect no, but you could still unit test your code if you refactor it slightly: