My AngularJS controller is structured as follows:
app.controller('MyCtrl', function($scope, service) {
$scope.positions = service.loadPositions(); // this invokes $http internally
$scope.save = function() {
...
};
// other $scope functions here
});
Whenever I write a test for any of the methods in $scope, I find that I need to stub service.loadPositions()
each time:
it('should save modified position', function($controller, service, $rootScope) {
spyOn(service, 'loadPositions').andReturn(fakeData);
var scope = $rootScope.$new();
$controller('MyCtrl', {$scope: scope});
// testing logic here
})
Is there a way to avoid repeating this stubbing process in every test? Once I have tested that an action is invoked on controller start, do I really need to stub it again in each subsequent test? This repetition is becoming cumbersome.
EDIT
I came across ngInit
and thought I could utilize it for this purpose, but it seems to be not recommended. I'm unsure why?