This sequence of lines:
ctrl = $controller('MainCtrl as mc', { $scope: $scope }); spyOn($scope.mc, 'loadData').and.callThrough();
Means that a Jasmine spy is created after the controller has already been created using $ controller. The init function has already been completed before creating the spy.
You also cannot switch between lines, because MainCtrl must exist before you can track a method on it.
If the init function calls another service, then look in that service method and confirm that the service is being called correctly. If MainCtrl just does something internally, then check the result of this, for example, by stating that the controller data / properties are being updated. It is not even worth checking if it is enough enough.
In addition, since you are using the controller as syntax, you can refer to the controller through the return value of the call to $ controller, and not directly access the scope:
ctrl = $controller('MainCtrl as mc', { $scope: $scope }); ctrl.loadData === $scope.mc.loadData; // true
user2943490
source share