Comment créer un test unitaire pour mon contrôleur LoginController AngularJS? Comment maquiller AuthenticationService pour cet exemple? Mon contrôleur est-il correctement construit pour les tests unitaires & en utilisant des maquettes?comment créer des tests unitaires pour les contrôleurs dans AngularJS
Controller Code LoginController:
layoutControllers.controller('LoginController',
['$scope', '$rootScope', '$location', 'AuthenticationService',
function ($scope, $rootScope, $location, AuthenticationService) {
AuthenticationService.ClearCredentials();
$scope.login = function() {
$scope.dataLoading = true;
AuthenticationService.Login($scope.email, $scope.password, function (response) {
AuthenticationService.SetCredentials($scope.email, $scope.password, response.UserId);
$scope._showValidationErrors(null, null);
$location.path('/');
}, function (response) {
$scope._showValidationErrors(response.Errors, response.OtherErrors);
});
$scope.dataLoading = false;
};
$scope._showValidationErrors = function (errors, otherErrors) {
$scope.validationErrors = [];
$scope.errors = {};
$scope.errors.form = {};
if (errors && angular.isArray(errors)) {
for (var errorCounter in errors) {
$scope.validationErrors.push(errors[errorCounter].Message);
$scope.errors.form[errors[errorCounter].Key] = errors[errorCounter].Message;
}
// debugger;
}
if (otherErrors && angular.isArray(otherErrors)) {
for (var errorCounter2 in otherErrors) {
$scope.validationErrors.push(otherErrors[errorCounter2]);
}
}
}
}]);
de ma connaissance :), ce n'est pas runing essai :). Qu'en est-il de la simulation du service AUthentication? Pas de cerdentails. Cela ne fonctionnera pas :) –
Cela va tester votre 'login()' fonction qui est votre code actuel. Vous pouvez capturer ses effets sur $ scope ou d'autres objets tels que $ location etc. Pouvez-vous décrire exactement ce que vous considérez comme une unité en test? – bhantol
Qu'en est-il du service Authetnication? c'est la dépendance. Je pensais que cela devrait être moqué pour tester seulement le contrôleur. Mais j'ai peut-être tort. –