私は基本的に以下のようなangularjsコントローラーを持っています
app.controller('MyCtrl', function($scope, service) {
$scope.positions = service.loadPositions(); // this calls $http internally
$scope.save = function() {
...
};
// other $scope functions here
});
$scope のいずれかのメソッドのテストを記述するたびに、service.loadPositions()
以下のようにスタブする必要があります。
it(should 'save modified position', function($controller, service, $rootScope) {
spyOn(service, 'loadPositions').andReturn(fakeData);
var scope = $rootScope.$new();
$controller('MyCtrl', {$scope: scope});
// test stuff here
})
すべてのテストでこの最初のスタブを回避する方法はありますか? つまり、コントローラーの起動時にこのアクションが呼び出されることを既にテストした場合、次のすべてのテストでこれをスタブする必要はありません。これにより、各テストで多くの繰り返しが発生します。
編集
つまずいngInit
たので使えると思ったのですが、こういう使い方はあまりおすすめできそうにないのですが、なぜだろう?