I'm trying to test a controller with a custom service dependency. The service has a dependency on $http. In my controller, I'm making reference to the "then" part of the promise, but that is making the test code blow up. How are you supposed to mock a service and test a controller dependent on it? Here's a plunker with the whole debacle. My service looks like this:
app.service('FooService', ['$http', function($http){
return {
getFoo: function(){
return $http.get('Foo');
},
getBar: function(){
return 'bar';
}
};
}]);
The controller:
app.controller('FooController', ['$scope', 'FooService', function(scope, FooService){
scope.hi = function() {};
scope.getFoo = function(){
FooService.getFoo().then(function(data){
scope.bar = data.data;
});
};
scope.getBar = function(){
scope.bar = FooService.getBar();
};
}]);
In my understanding, the $http dependency should not be on the controller in order to drive testability and separation of concerns. However, now I'm having trouble with the promise... How should this be done properly?