Mocking $http during Unit testing

Unit tests are meant to test the pieces of code we have written. They are not expected to verify the responses from external systems. Hence, in situations where our code needs to make external requests, we simply fake the request and respond with a canned response. This is called mocking, and in our case, we will use the $httpBackend service to mock our $http requests.

We start by replacing the code in the test/unit/servicesSpec.js file with the following lines of code:

'use strict';
describe('service', function() {
  beforeEach(module('myApp.services'));
  describe('rtmFactory', function() {

  })
})

Now, within the rtmfactory function, we will declare some objects and create our function that we would like to be injected ...

Get AngularJS Web Application Development Blueprints 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.