I'm going through the AngularJS tutorial step 5, and came across this snippet in the testing section:
describe('PhoneCat controllers', function() {
describe('PhoneListCtrl', function(){
var scope, ctrl, $httpBackend;
// Load our app module definition before each test.
beforeEach(module('phonecatApp'));
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
// This allows us to inject a service but then attach it to a variable
// with the same name as the service.
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('phones/phones.json').
respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
scope = $rootScope.$new();
ctrl = $controller('PhoneListCtrl', {$scope: scope});
}));
I don't fully understand the purposes of the underscore when injecting $httpBackend
. I see the comment and understand what the code is doing. I just don't get why we are only doing this with $httpBackend
.
There are two other services we are injecting right along with it that don't need to be injected this way. How are we helping ourselves by injecting $httpBackend
in a roundabout manner then immediately assigning it to a variable of the same name, couldn't we just inject it directly?