Testing Controllers That Use Angular-Resource

Before Chapter 6, Test This Fancy New Code, our Angular code was using $http to make Ajax calls. Our tests used $httpBackend to mock out the HTTP interactions between our controller and the server. Will that still work, now that we are using angular-resource?

The short answer is yes. Because angular-resource is using $http under the covers, the same techniques apply. In fact, you can use this information to refactor CustomerSearchController to use angular-resource.

Because angular-resource requires us to understand promises, let’s walk through a basic test of the CustomerCreditCardController, so it’s clear how promises interact with our tests.

First, we’ll set up our tests in the usual way: ...

Get Rails, Angular, Postgres, and Bootstrap now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.