In one of my unit tests I'm trying to do
beforeEach(function () {
angular.mock.inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
mockUserResource = $injector.get('User');
$httpProvider = $injector.get('$httpProvider'); // <-- problem
$httpProvider.interceptors.push('myInterceptor');
});
});
(demo)
Why is it not possible to inject $httpProvider ?
The reason I'm doing this is because I don't load the file which adds all my interceptors, because I want to test them one by one!
Providers are only injectable in the config phase, angular.config
.
According to this answer, you could try something like:
beforeEach(module('yourModule', function($httpProvider) {
$httpProvider.interceptors.push('myInterceptor');
}));
beforeEach(function() {
angular.mock.inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
mockUserResource = $injector.get('User');
});
});
For more information on how dependency injection works, this is a great article (not written by the AngularJS team)
The name Provider
is automatically added by angular. So, you should just inject $http
.
$httpProvider = $injector.get('$http');
here is a working fork of your fiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With