The module definition
var module = angular.module('test', []);
module.provider('client', function() {
this.$get = function($http) {
return {
foo: function() {
return $http.get('foo');
}
}
}
});
module.factory('service', ['client', function(client) {
return {
bar: function() {
return client.foo();
}
}
}]);
Essentially, the "client" serves as a wrapper for making HTTP calls, while the "service" wraps around basic features of the client.
I am conducting unit tests on both the provider and service using karma+jasmine. The provider tests are running smoothly, but I'm encountering an issue with the service tests:
describe('service test', function(){
var service = null;
beforeEach(function(){
module('test')
inject(function(_service_, $httpBackend, $injector) {
service = _service_;
$httpBackend = $injector.get('$httpBackend');
});
});
it('should call client.foo via service.bar', function() {
$httpBackend.expect("GET", "foo");
service.bar();
expect($httpBackend.flush).not.toThrow();
});
});
I am receiving the error message
Expected function not to throw, but it threw Error: No pending request to flush !.
. However, when testing the provider in the same manner, the test passes. Why is that?