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 before every test.
Continue by adding the following code to the same function:
var scope, httpBackend, rtmFactory, result;
beforeEach(inject(function(_rtmFactory_, $httpBackend) {
httpBackend = $httpBackend;
rtmFactory = _rtmFactory_...