O'Reilly logo

Mastering Web Application Development with AngularJS by Pawel Kozlowski, Peter Bacon Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Testing code that interacts with $http

Testing code calling external HTTP services is often problematic due to network latency and changing data. We want our test to run fast and be predictable. Fortunately, AngularJS provides excellent mocks to simulate HTTP responses.

In AngularJS the $http service depends on another, lower-level service $httpBackend. We can think of the $httpBackend as a thin wrapper over the XMLHttpRequest object. This wrapper masks browsers incompatibilities and enables JSONP requests.

Note

Application code should never call the $httpBackend directly as the $http service provides a much better abstraction. But having a separate $httpBackend service means that we can swap it for a mock one during testing.

To see the $httpBackend ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required