Hello,
The dependency injection in tests is confusing to me.
Specifically in chapter 5 <https://docs.angularjs.org/tutorial/step_05#test> of
tutorial, I can see that $httpBackend is configured to provide a fake
response:
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('phones/phones.json').
respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
But I can't see why the controller wants to use this newly defined
dependency instead of the original $http introduced in PhoneListCtrl
definition.
In the same code snippet, if the controller was initialized with something
like this:
scope = $rootScope.$new();
ctrl = $controller('PhoneListCtrl', {
$scope: scope,
$http: $httpBackend});
I could understand that we are overriding the dependency so when the
controller wants to access $http, it will use our mocked one.
But given that't not the case:
scope = $rootScope.$new();
ctrl = $controller('PhoneListCtrl', {$scope: scope});
And only the scope is passed to $controller and I can't see anything that
would link the $httpBackend to the controller.
I am just confused how the controller know about the new mocked
service/dependency.
Any hint is appreciated.
Best,
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.