I am new to AngularJS unit testing and this is a best practices question. If I have a data factory and a controller in separate files, and the controller calls the factory, do I need to write test specs for both the factory and the controller. Example
Controller method and test:
// controller method ------------------------------------------------------
$scope.getAsset = function (id) {
if ($scope.id != '0') {
assetFactory.getAsset($scope.id)
.then(function (response) {
$scope.asset = response.data;
//$scope.selectedDepartmentName = $scope.asset.DepartmentName;
})
.catch(function (error) {
alertService.add('danger', 'Unable to load asset data: ' + error.statusText + '(' + error.status + '). Please report this error to the application administrator');
});
}
};
// test ------------------------------------------------------
beforeEach(inject(function (assetFactory) {
spyOn(assetFactory, 'getAsset').and.callThrough();
}));
it('method getAsset() was called', inject(function (assetFactory) {
scope.getAsset();
expect(assetFactory.getAsset).toHaveBeenCalled();
}));
Factory method and test:
// factory method ------------------------------------------------------
factory.getAsset = function (id) {
url = baseAddress + "asset/get/" + id;
return $http.get(url);
};
// test ------------------------------------------------------
beforeEach(inject(function (assetFactory) {
spyOn(assetFactory, 'getAsset').and.callThrough();
}));
it('method getAsset() was called', inject(function (assetFactory) {
assetFactory.getAsset();
expect(assetFactory.getAsset).toHaveBeenCalled();
}));
The controller test spec is already testing the factory call. I could see if I had a factory not being called by a controller and maybe that is the argument for testing both. In fact, in this application all the factories are called within one or more controllers.
On the other hand, maybe I test all my factories and just make sure the controller methods that call the factories are defined, not even try to spy on them in the controller.
Any advice is appreciated.